是否有办法访问自定义路线参数,方法与路线“名称”相同:'cache'=>true
Route::GET('tools/languages/{page?}', array('uses'=> 'Tools@list_languages', 'as'=>'list_languages', 'cache'=>true));
如何从Controller访问cache
值?
感谢,
答案 0 :(得分:1)
是的,您可以从Middleware获取您的Route参数。
在您的中间件中,您可以获得匹配的路由对象"像这样:
class MyMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$matchedRouteObject = $request->route();
$next($request);
}
}
请参阅print_r($request->route())
在此Route对象中有一个名为action的属性。 action属性具有匹配Route的所有参数。
routes/web.php :
Route::get('tools/languages/{page?}', [
'uses' => 'Tools@list_languages',
'middleware' => 'App\Http\Middleware\MyMiddleware',
'cache' => 'value'
]);
app / Http / Middleware / MyMiddleware.php:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Response;
class MyMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$matchedRouteObject = $this->route();
$deedVariable = $mathedRouteObject->action['cache']; // here you got your variable.
return $next($request);
}
}
答案 1 :(得分:0)
扩展@Exprator应答,您可以访问控制器中的参数
public function list_languages(Request $request)
{
$request->route()->getAction()['cache']; // returns true
}
https://laravel.com/api/5.4/Illuminate/Routing/Route.html#method_getAction