从 url、laravel 获取控制器动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20281420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:43:03  来源:igfitidea点击:

Get controller action from url, laravel

phplaravelfilterlaravel-4laravel-routing

提问by aayush shrestha

Can I get the controller action from given URL?

我可以从给定的 URL 获取控制器操作吗?

In my project, I will have different layout used for admin and normal users. i.e.

在我的项目中,我将为管理员和普通用户使用不同的布局。IE

something.com/content/list - will show layout 1.

something.com/admin/content/list - will show layout 2.

(But these need to be generated by the same controller)

something.com/content/list - 将显示布局 1。

something.com/admin/content/list - 将显示布局 2。

(但这些需要由同一个控制器生成)

I have added filter to detect the pattern 'admin/*'for this purpose. Now I need to call the action required by the rest of the URL ('content/list' or anything that will appear there). Meaning, there could be anything after admin/it could be foo/1/edit(in which case foo controller should be called) or it could be bar/1/edit(in which case bar controller should be called). That is why the controller name should be generated dynamically from the url that the filter captures,

'admin/*'为此,我添加了过滤器来检测模式。现在我需要调用 URL 的其余部分(“ content/list”或将出现在那里的任何内容)所需的操作。意思是,在admin/它之后可能有任何东西foo/1/edit(在这种情况下应该调用 foo 控制器)或者它可能是bar/1/edit(在这种情况下应该调用 bar 控制器)。这就是为什么应该从过滤器捕获的 url 动态生成控制器名称的原因,

So, I want to get the controller action from the URL (content/list) and then call that controller action from inside the filter.

所以,我想从 URL(内容/列表)中获取控制器操作,然后从过滤器内部调用该控制器操作。

Can this be done?

这能做到吗?

回答by aayush shrestha

Thanks to everyone who participated.

感谢所有参加的人。

I just found the solution to my problem in another thread. HERE

我刚刚在另一个线程中找到了解决我的问题的方法。这里

This is what I did.

这就是我所做的。

if(Request::is('admin/*')) {
    $my_route = str_replace(URL::to('admin'),"",Request::url());

    $request = Request::create($my_route);
    return Route::dispatch($request)->getContent();
}

I could not find these methods in the documentation. So I hope, this will help others too.

我在文档中找不到这些方法。所以我希望,这也能帮助其他人。

回答by egig

You can use RESTful Controller

您可以使用RESTful 控制器

Route:controller('/', 'Namespace\yourController');

But the method have to be prefixed by HTTP verb and I am not sure whether it can contain more url segment, in your case, I suggest just use:

但是该方法必须以 HTTP 动词为前缀,我不确定它是否可以包含更多 url 段,在您的情况下,我建议只使用:

Route::group(array('prefix' => 'admin'), function()
{
    //map certain path to certain controller, and just throw 404 if no matching route  
    //it's good practice
    Route::('content/list', 'yourController@yourMethod');

});

回答by Anil Saini

Use this in your controller function -

在您的控制器功能中使用它 -

if (Request::is('admin/*'))
{
    //layout for admin (layout 2)
}else{
    //normal layout (layout 1)
}

回答by The Alpha

You can use Request::segment(index)to get part/segment of the url

您可以使用Request::segment(index)获取部分/段url

// http://www.somedomain.com/somecontroller/someaction/param1/param2
$controller = Request::segment(1); // somecontroller
$action = Request::segment(2); // someaction
$param1 = Request::segment(3); // param1
$param2 = Request::segment(3); // param2