Jump to content

Call a controller function from view


Nedyc

Recommended Posts

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();

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
   }
?>

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...