Build your own PHP framework-Abstract The Controller base class
We will resolve and execute simple routes and migrate them from the public/index. php file to the framework. The entry file becomes refreshing ~~ However, go to our controller and check the following code: public function actionView () {$ body = 'test body information'; require '.. /views/site/view. php ';} is every time we write an action to render the page, we need to find the view in the corresponding path and then request it in. This is definitely not the case. Therefore, we need to abstract a base class of Controller to implement a method for rendering pages, so that other controllers can inherit them and use the corresponding method. Needless to say, the controller's base class must be written into the framework. In addition, two parameters must be written, one in the base, the other in the web, and the Controller in the web inherits from the base. Let's look at
<?phpnamespace sf\base;/** * Controller is the base class for classes containing controller logic. * @author Harry Sun <sunguangjun@126.com> */class Controller{}
There is only one empty class, waiting to add content. Let's take a look
<?phpnamespace sf\web;/** * Controller is the base class for classes containing controller logic. * @author Harry Sun <sunguangjun@126.com> */class Controller extends \sf\base\Controller{ /** * Renders a view * @param string $view the view name. * @param array $params the parameters (name-value pairs) that should be made available in the view. */ public function render($view, $params = []) { extract($params); return require '../views/' . $view . '.php'; }}
As you can see, we first import the variables from the array to the current symbol table, and then introduce the corresponding view page. Then, in SiteController, we only need to write it like this.
<?phpnamespace app\controllers;use sf\web\Controller;class SiteController extends Controller{ public function actionTest() { echo 'success!'; } public function actionView() { $this->render('site/view', ['body' => 'Test body information']); }}
Then, visit www.bkjia.com to see the same page as before. Let's improve the Controller in the base.
<?phpnamespace sf\base;/** * Controller is the base class for classes containing controller logic. * @author Harry Sun <sunguangjun@126.com> */class Controller{ /** * @var string the ID of this controller. */ public $id; /** * @var Action the action that is currently being executed. */ public $action;}
Two attributes are added to record the current controller and action respectively. Then, assign a value to the router after parsing it. The code is as follows:
<?phpnamespace sf\web;/** * Application is the base class for all application classes. * @author Harry Sun <sunguangjun@126.com> */class Application extends \sf\base\Application{ /** * Handles the specified request. * @return Response the resulting response */ public function handleRequest() { $router = $_GET['r']; list($controllerName, $actionName) = explode('/', $router); $ucController = ucfirst($controllerName); $controllerNameAll = $this->controllerNamespace . '\\' . $ucController . 'Controller'; $controller = new $controllerNameAll(); $controller->id = $controllerName; $controller->action = $actionName; return call_user_func([$controller, 'action'. ucfirst($actionName)]); }}
Then we can get the corresponding controller name and action name in the controller and view, and modify view. php as follows:
Then we can see the following page. Some people think that everyone is now separating the frontend and backend. We don't need to use PHP to render a page. We just need to return a josn string, this is simpler. Add a toJson Method to the web Controller.
/** * Convert a array to json string * @param string $data */ public function toJson($data) { if (is_string($data)) { return $data; } return json_encode($data); }
Modify actionTest in SiteController as follows: public function actionTest () {$ data = ['first' => 'awesome-php-zh_CN ', 'second' => 'simple-framework']; echo $ this-> toJson ($ data );}