国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

webman自定義請求對象,控制器如何調(diào)用

wadeYang

看文檔
基礎功能-請求-自定義請求對象
1、新建 app/Request.php
2、在 config/server.php 中增加配置

沒有自定義之前是這樣用

use support\Request;
class Index
{
    public function index(Request $request)
    {
        return response('hello webman');
    }
}

自定義app/Request.php后,控制器如何用??,請教一下各位

2136 4 0
4個回答

admin

use app \Request;
即可,因為會自動注入

public function index(app\Request $request)

  • 暫無評論
nitron
TypeError: app\controller\User::hello2(): Argument #1 ($request) must be of type app\controller\app\Request, support\Request given

你引用的Request錯了,提示很明顯了 app\controller\app\Request

把hello2(app\Request $req)改成hello2(Request $req)
  • 暫無評論
wadeYang

已經(jīng)解決該問題,文檔有誤。正確操作如下

1、新建app/MyRequest.php,為了明顯區(qū)別系統(tǒng)體提供的support\Request類,我換了個名字MyRequest

<?php
namespace app;

class MyRequest extends \support\Request
{
    public function get($name = null, $default = null)
    {
        var_dump(222);
        return $this->filter(parent::get($name, $default));
    }

    public function post($name = null, $default = null)
    {
        return $this->filter(parent::post($name, $default));
    }

    public function filter($value)
    {
        if (!$value) {
            return $value;
        }
        if (is_array($value)) {
            array_walk_recursive($value, function(&$item){
                if (is_string($item)) {
                    $item = htmlspecialchars($item);
                }
            });
        } else {
            $value = htmlspecialchars($value);
        }
        return $value;
    }
}

2、在 config/app.php 中增加如下配置。(文檔說的是在config/server.php增加,此處錯誤)

return [
    // ... 這里省略了其它配置 ...
    'request_class' => app\MyRequest::class, //注意這里是,一定要引入第一步新增的類
];

3、控制器中使用

<?php
namespace app\controller;

// use support\Request;
use app\MyRequest;

class User
{
    public function hello2(MyRequest $request)
    {

        $default_name = 'webman';
        var_dump(111);
        $name =  $request->get('name', $default_name);
        return view('user/hello', ['name' => $name]);
    }

}
  • 暫無評論
法師

文檔沒問題,使用的時候 use support\Request; 改成 use app\Request; 就行了,命名空間是php基礎了

use app\Request;
class Index
{
    public function index(Request $request)
    {
        return response('hello webman');
    }
}
  • 暫無評論
年代過于久遠,無法發(fā)表回答
??