Nedyc Posted April 8, 2011 Report Share Posted April 8, 2011 Hi, into the index controller I've a public function like this public function filemanagerAction(){ function blabla(){ echo "LOL"; } } how can I call it from the view? Quote Link to comment Share on other sites More sharing options...
BeeDev Posted April 8, 2011 Report Share Posted April 8, 2011 require_once("path/to/index-controller.php"); filemanagerAction(); Or if the filemanagerAction() function is in a class then you can call it with following without instancing the class: require_once("path/to/index-controller.php"); className::filemanagerAction(); Quote Link to comment Share on other sites More sharing options...
Nedyc Posted April 8, 2011 Author Report Share Posted April 8, 2011 Well, I did follow the killerphp.com framework tutorial, so I'm working with something like codeIgniter and controllers are automatically dispatched; My problem is how to recall a function inside a controller from its view! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted April 8, 2011 Report Share Posted April 8, 2011 My two cents, in case no one else responds... In my view, if you have to call the controller from the view, you are probably doing something structurally wrong with your application. I try to keep my models/controllers/views completely separate. My controllers interface with models/views, but neither the models or the views can call the controllers, since that would mean that the view is doing the job of the controller. In my case, I usually do any controller calls necessary before I load the view. If the view will need some specific data, I will take care of that before I load the view, create a variable for any resulting data, and pass those variables into the view. Perhaps if you can give a more complete description of what you need (I know you are wanting to call a controller function from a view, but why do you need to do that? What is the user doing that makes this necessary?) I can help more. Quote Link to comment Share on other sites More sharing options...
Nedyc Posted April 8, 2011 Author Report Share Posted April 8, 2011 Well, suppose I want to display folders and files inside a specific folder called 'fileManager' With the controller I get files/folders name and save their name into an array called $this->content Into the view section I'll do <?PHP for($i=0;$i<count($this->content);$i++){ ?> <div><?=$this->content[$i]?></div> <?php } ?> Now i want to color of blue the second div, the forth, sixth...ecc... to graphically give a nice effect, so I thought to create a function like this and put it into the controller to separate code from views <?PHP function rowColor($colorClass){ if($colorClass = '') $colorClass = 'colored'; else $colorClass = ''; return $colorClass; } And then modify the first part of code: <?PHP $class = ''; for($i=0;$i<count($this->content);$i++){ ?> <div class="<?=$class?>"><?=$this->content[$i]?></div> <?php $class = rowColor($class); } ?> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted April 8, 2011 Report Share Posted April 8, 2011 To be honest, I'd wouldn't make a separate call to the constructor just for this. I'd just do this: <?PHP for($i=0;$i<count($this->content);$i++){ ?> <div<?php if ($i % 2 == 0) { echo " class='colored'"; }?>><?=$this->content[$i]?></div> <?php } ?> If you really need to make a separate function for this, I would create a new file that holds functions that your view files use, and include that file/call the function that way. It doesn't make much sense to me to include utility functions like this in the controller (especially if this means duplicating code, if your views use the same functions but have different controllers.) Quote Link to comment Share on other sites More sharing options...
Nedyc Posted April 9, 2011 Author Report Share Posted April 9, 2011 Your way is 1000x smarter than mine Thanks a lot!!! Andrea Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.