Laravel Illuminate Request 方法扩展

实现

Request::macro('appid', function()
{
    return $this->input('app-id');
}

使用

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class IndexController  extends Controller
{
    public function test(Request $request)
    {
    	$request->appid();
    }	
}

文献:

  • (官方) Laravel Extending Collections#macro
  • (learnku社区) Laravel Extending Collections#macro
  • Laravel Macro 让你的代码更简洁,更具有可读性

扩展(TP方面有想了解的可以在评论区留言,以后跟进):

// 构建一个Request扩展类,继承think\Request
namespace app\extend;
use think\Request as SysRequest

class Request extends SysRequest {
	public function appid()
    {
        return $this->input('app-id');
    }
}
// 修改provider.php配置
return [
	// ...
    'request '      => \app\extend\Request::class,
    // ...
];
  • Thinkphp 6 容器和依赖注入-批量绑定
  • Thinkphp 8 容器和依赖注入-批量绑定

你可能感兴趣的:(#,Laravel/Lumen,laravel,android,php)