将中间件绑定到laravel 5中的控制器

将中间件绑定到laravel 5中的控制器,laravel,laravel-5,Laravel,Laravel 5,我想在我的控制器中使用中间件,但我事先不知道路径,因为我从数据库中获取slug 在拉威尔5号有可能吗 提前感谢您在控制器的构造函数中,执行以下操作: public function __construct() { //This will apply to every method inside the controller $this->middleware('auth'); //This will apply only to the methods listed

我想在我的控制器中使用中间件,但我事先不知道路径,因为我从数据库中获取slug

在拉威尔5号有可能吗


提前感谢您

在控制器的构造函数中,执行以下操作:

public function __construct()
{
    //This will apply to every method inside the controller
    $this->middleware('auth');

    //This will apply only to the methods listed inside the array
    $this->middleware('log', ['only' => ['fooAction', 'barAction']]);

    //This will apply only to the methods except the ones listed inside the array
    $this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
}

您可以在控制器的构造器中阅读此信息,请执行以下操作:

public function __construct()
{
    //This will apply to every method inside the controller
    $this->middleware('auth');

    //This will apply only to the methods listed inside the array
    $this->middleware('log', ['only' => ['fooAction', 'barAction']]);

    //This will apply only to the methods except the ones listed inside the array
    $this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
}

你可以读到这个

是的,是的。你想要实现什么?你们以前试过什么吗?我在管理端使用中间件,所有的路由都是已知的,并且可以工作,但在站点端,我相应地使用slug和bind控制器。在这种情况下,中间件不起作用。全球中间件是,但我想在某些控制器上。非常感谢。首先,根据需要创建中间件,然后使用不同的方法调用middle,如:
Route::get('admin/profile',['middleware'=>'auth',function(){}])
$this->中间件('auth')在控制器结构中。是的,它是。你想要实现什么?你们以前试过什么吗?我在管理端使用中间件,所有的路由都是已知的,并且可以工作,但在站点端,我相应地使用slug和bind控制器。在这种情况下,中间件不起作用。全球中间件是,但我想在某些控制器上。非常感谢。首先,根据需要创建中间件,然后使用不同的方法调用middle,如:
Route::get('admin/profile',['middleware'=>'auth',function(){}])
$this->中间件('auth')在你的控制器结构中。我对已知的路由使用这样的中间件,它正在工作,但是如果我以前不知道路由,它就不工作了。我得到了那个部分,但你至少要知道你站点的结构。你有一个slug,所以你必须有类似于/some_route/{slug}或类似的东西。最坏的情况是,只需在控制器中为每个路由触发一个方法,并应用一些逻辑来重定向。显示routes.php文件以提供更多帮助MyController@myMethod'); -  在这种情况下,中间件当然可以工作。我在MyController中有它,比如$this->middleware('name');“路由::获取('{lang}/{slug}/{params?}',”PagesController@getPage“)->其中('params',”(.*);“在这种情况下,PagesController决定加载哪个控制器。当它在这里调用MyController时,中间件不起作用,因为它依赖于路由,它是逻辑的。非常感谢您的时间:))我使用像这样的中间件来处理已知的路由,它正在工作,但是如果我以前不知道路由,它就不工作了。我知道这一部分,但您至少要知道站点的结构。你有一个slug,所以你必须有类似于/some_route/{slug}或类似的东西。最坏的情况是,只需在控制器中为每个路由触发一个方法,并应用一些逻辑来重定向。显示routes.php文件以提供更多帮助MyController@myMethod'); -  在这种情况下,中间件当然可以工作。我在MyController中有它,比如$this->middleware('name');“路由::获取('{lang}/{slug}/{params?}',”PagesController@getPage“)->其中('params',”(.*);“在这种情况下,PagesController决定加载哪个控制器。当它在这里调用MyController时,中间件不起作用,因为它依赖于路由,它是逻辑的。非常感谢您抽出时间:))