Skip to content
やかみそら edited this page Aug 9, 2017 · 23 revisions

Controller

What is a controller?

In short, a controller is a class file.

When the name of the controller matches the route you set, it will be loaded.

Let's try: Hello World!

Then you will see how to create a simple controller, open your text editor, create a new file Blog.php, and then add the following code:

namespace app\controllers;

use Kotori\Core\Controller;

class Blog extends Controller {

    public function index()
    {
        echo 'Hello World!';
    }
}

Then save the file to the app/controllers directory.

Added in configuration $config['url_route'] = array('/' => 'Hello/index');

Now use the URL similar to the following to access your site:

http://example.com/

If everything is fine, you will see:

Hello World!

Also, make sure that your controller inherits the parent controller class so that it can use the parent class method.

Generating URL

In order to match the URL format, we need to be able to dynamically generate the corresponding URL address based on the current URL settings. To this end, the built-in url method is provided for dynamic generation of URLs to ensure that the project is not affected by the environment during the migration process.

It is recommended that you use this method at any time to generate your URL so that your code will be portable when your environment changes.

The URI segment parameters which passed to the function can be a string, it can also be an array, the following is an example of a string:

$this->route->url('news/123');

The example above returns something like:

http://example.com/news/123

Here's an example of using an array:

echo $this->route->url(array('news', '123'));

If you configure a multi-domain name, you must hope that we can generate url base on multi-domain:

echo $this->route->url('test', 'module1');

Will return to the module1 corresponding to the domain name stitching generated url address.

JSON outputs

Used to return JSON formated data.

Example:

$array['name'] = 'MahuaTeng';
$array['length'] = 1;
$this->response->throwJSON($array);

Redirection

301/302 Redirect to a page

Example:

$this->response->redirect('http://www.qq.com', true);// redirect to QQ
// the second parameter determines whether 301 or 302

Accept safe variables

In the process of Web development, we often need to obtain the system variables or user submitted data, these data are complex, and accidentally easy to cause security risks, but you can easily get and control the variables with our framework.

Example:

    $id    =  $this->request->get('id'); // $_GET['id']
    $name  =  $this->request->post('name');  // $_POST['name']
    $file  =  $this->request->server('PHP_SELF'); // $_SERVER['PHP_SELF']
    $get   =  $this->request->get(); // $_GET
              $this->request->cookie('name', 'value'); // $_COOKIE['name'] = 'value';
    $name  =  $this->request->cookie('name'); // $_COOKIE['name']
              $this->request->cookie('name', null); // delete $_COOKIE['name']
              $this->request->cookie(null); // delete $_COOKIE
    $name  =  $this->request->session('name'); // $_SESSION['name']
              $this->request->session('name', null); // delete $_SESSION['name']
              $this->request->session(null); // delete $_SESSION

Cache

Sorry, document is not ready.

Running in CLI mode

Let's try: Hello World!

Let's first create a simple controller, open your text editor, create a new file and name it Tools.php, and then enter the following code:

namespace app\controllers;

use Kotori\Core\Controller;

class Tools extends Controller {

    public function message($to = 'World')
    {
        echo "Hello {$to}!".PHP_EOL;
    }
}

Added in the configuration file $config['url_route'] = array('tools/(.*)' => array('cli' => 'Tools/message/$1'));

We can access through the CLI. In Mac/Linux you can open a terminal, in Windows you can open the "run", and then enter "cmd", then go to the project directory.

$ cd /path/to/project;
$ php index.php tools/kotori

If you are working correctly, you should see `Hello kotori! '.

Error page

When an error occurs in your system, an error page is output. If you open the app_debug mode, it will be shown in the lower right corner of the view, making it easy to debug. If you need to customize the error template, please refer to views/Public/error.html and modify. It is important to note that the error message will no longer be displayed after closing app_debug.

Clone this wiki locally