开发者

call controller action from a view in php

开发者 https://www.devze.com 2023-03-08 05:51 出处:网络
In PHP while working through MVC pattern without any framework when the page is loaded and for CRUD processing again the file should go to the controller. So how to connect view page to the controller

In PHP while working through MVC pattern without any framework when the page is loaded and for CRUD processing again the file should go to the controller. So how to connect view page to the controller as the view page is simple like HTML? Controller is rendering the view page through ActionView so how the view page will be link to the controller for CRUD processin开发者_运维百科g.


Each MVC request goes through the same Controller -> Model -> View cycle. In this sense, every view is always "contacting" a controller, just not in the same request.

  1. user opens URL /posts/add
  2. the PostsController grabs some data and renders the view
  3. the view consists of a form, possibly displaying some data from the controller
  4. the final HTML is sent to the user

One request cycle ends here. The browser displays the HTML form. When submitting the form, a new request is sent to the server.

  1. form is posted to /posts/add
  2. the PostsController grabs the data, saves it using the model
  3. if everything worked, the controller renders a "Thank you" page (or whatever else it does)
  4. if it didn't work, the controller renders the form view again, populated with error messages
  5. the final HTML is sent to the user

This completes the cycle again. The view never talks to the controller inside the MVC cycle. It just outputs HTML which the user sees. The user then initiates a new request to the controller.


Say you have the following:

/m/User.class.php /v/newUser.php /c/UserController.class.php

I usually break those down into 3 layers: the model in a class, a well encapsulated high-cohesive class for a given business object. While this class is responsible for the user's object model, the controller is responsible for implementing the CRUD methods. This makes a simple separation of concerns, promotes code reuse, etc. Then, you can plug the controller at any view you need, say "newUser.php" to view users.

Since PHP is a script language, you need to import the dependencies before its use. So, looking at the dependency tree, I usually import the model into the controller using "require_once", and the controllers to the view.

User.class.php ----- class User { private $username;

 User($newUsername) {
     $this->username = $newUsername;
 }

}

userController.php --------------- (Considering the direction "app" below is under the PHP INCLUDE_PATH variable)

require_once("app/m/User.class.php");

class UserController {

public static createUser($newUsername) {
    return new User($newUsername);
}

}

newUser.php ---------- (Considering the direction "app" below is under the PHP INCLUDE_PATH variable)

require_once("app/c/UserController.class.php");

$newUser = $UserController.createUser("username");

echo "<b>" . $newUser;

In addition to this, I use an ORM system like Propel to provide the persistence to user. I had implemented a large PHP system during my Masters with Propel and manual MVC... For a more complete example of this structure go to http://java.net/projects/infinity-metrics/sources/svn/content/trunk/app/classes/infinitymetrics/controller/UserManagementController.class.php for an example of a Controller. Here's an example of a view using this controller: http://java.net/projects/infinity-metrics/sources/svn/content/trunk/app/user/student/signup-step1.php.

0

精彩评论

暂无评论...
验证码 换一张
取 消