We move the simple routing parsing and execution from the portal file public/index.php to the framework. The entry file suddenly became very refreshing ~ ~ ~ But, to our controller, we will see the following code:public function Actionview () {$body = ' Test body inf Ormation '; Require '.. /views/site/view.php '; Every action we write to render the page, we go to the view of the corresponding path and require it in. Certainly not, so we want to abstract a controller base class, implement a rendering page method, let other controller inheritance, you can use the corresponding method. Needless to say, the base class of this controller must be written into the framework. And also write two, one in base, one on the web, the controller in the web inherits from base. First look at the base in the
<?php
namespace 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. Then look at the Web
<?php
namespace 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 is 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 into the current symbol table, and then we introduce the corresponding view page. And then, in Sitecontroller, we just have to write it.
<?php
namespace 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, to visit www.2cto.com, you can see the same page as before. Let's refine the controller in base.
<?php
namespace 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 is currently being executed.
* * Public
$action;
}
Two attributes were added to record the current controller and action. Then, after parsing the router, we assign the value to the following code:
<?php
namespace Sf\web;
/**
* Application is the base class of 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 controller and view, and change the view.php as follows:
And then we can see the following page. Some people think that now everyone is separated, we do not need to use PHP to render a page, just return a josn string is good, this is simpler, in the Web controller add a Tojson method can
/**
* Convert a array to JSON string
* @param string $data
/Public Function Tojson ($data)
{
if (is_string ($data)) {return
$data;
}
Return Json_encode ($data);
Change the actiontest in the Sitecontroller to the following: Public function actiontest () {$data = [' ' The ' => ' Awesome-php-zh_ CN ', ' second ' => ' simple-framework ']; echo $this->tojson ($data); }