thinkPHP controller defines

ThinkPHP V5.0 controller defines more flexible, you may not need to inherit any of the base class can inherit an official package of \think\Controllerclasses or other controller class.

Controller defines

A typical controller class is defined as follows:

namespace app\index\controller;

class Index 
{
    public function index() { return 'index'; } } 

The actual location of the controller class file is

application\index\controller\Index.php

The controller class may not need to inherit any class, with the default namespace appis the root namespace.

Root namespace controller can be set, for example, we modify the application configuration file:

// 修改应用类库命名空间
'app_namespace' => 'application', 

V5.0.8+Version, app_namespaceconfiguration parameters changed APP_NAMESPACEconstants defined in the entry file.

The actual controller class should be changed is defined as follows:

namespace application\index\controller;

class Index 
{
    public function index() { return 'index'; } } 

Just change the name space, but the actual file location and name has not changed.

This method allows the controller class definition, if you want to render a template in the controller inside, you can use

namespace app\index\controller;

use think\View;

class Index 
{
    public function index() { $view = new View(); return $view->fetch('index'); } } 

Or directly using view helper to render template output, for example:

namespace app\index\controller;

class Index 
{
    public function index() { return view('index'); } } 

If the inherited think\Controllerclass, you can directly call think\Viewand think\Requestmethods of the class, for example:

namespace app\index\controller;

use think\Controller;

class Index extends Controller
{ public function index() { // 获取包含域名的完整URL地址 $this->assign('domain',$this->request->url(true)); return $this->fetch('index'); } } 

Render Output

By default, the output of the controller used in all returnthe way, without any manual output, the system will automatically output rendered content.

The following are valid Output:

namespace app\index\controller;

class Index 
{
    public function hello() { return 'hello,world!'; } public function json() { return json_encode($data); } public function read() { return view(); } } 

The controller generally does not require any output directly return to.

Output conversion

By default, the controller do not return any output data processing, but the output format may be provided, and the automatic data conversion process, provided that the output data from the controller must be returnthe way to return.

If the controller is defined as:

namespace app\index\controller;

class Index 
{
    public function hello() { return 'hello,world!'; } public function data() { return ['name'=>'thinkphp','status'=>1]; } } 

When we set the output data format JSON:

// 默认输出类型
'default_return_type'   => 'json', 

We visit

http://localhost/index.php/index/Index/hello http://localhost/index.php/index/Index/data 

The results output becomes:

"hello,world!"
{"name":"thinkphp","status":1} 

By default, the controller will return in ajax request type is automatically converted, the default is json

If we define the controller

namespace app\index\controller;

class Index 
{
    public function data() { return ['name'=>'thinkphp','status'=>1]; } } 

We visit

http://localhost/index.php/index/Index/data 

The results output becomes:

{"name":"thinkphp","status":1} 

When we set the output data format is html:

// 默认输出类型
'default_ajax_return'   => 'html', 

In this case the request will not return ajax content conversion

Guess you like

Origin www.cnblogs.com/tyblwmbs/p/10967509.html