Can I get the controller action from given URL?

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

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)

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 afteradmin/it could befoo/1/edit(in which case foo controller should be called) or it could bebar/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,

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

Can this be done?

shareimprove this question
 
  
How did you created routes for these ? Can you post the routes related to these ?The AlphaNov 29 '13 at 8:56
  
Can you show the routes for these ?The AlphaNov 29 '13 at 9:04

4 Answers 正确答案

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();
}

我的做法如下 
$request_direct = Request::create('baikes/index'); //dd($request_direct); 
return Route::dispatch($request_direct)->getContent();

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

shareimprove this answer
 

Use this in your controller function -

if (Request::is('admin/*'))
{
    //layout for admin (layout 2)
}else{
    //normal layout (layout 1)
}
shareimprove this answer
 
  
I have done this already. Now, I need to look at what is found after "admin/" and then call the controller corresponding to that url. Look at the edit i have posted in question.aayush shresthaNov 29 '13 at 8:51
  
read the individual uri segment using$segment = Request::segment(1);and then redirect accordingly. Like change the parameter value like 1,2 .. depending upon the parameter you need to use to decide for redirection.Anil SainiNov 29 '13 at 8:55
  
Redirect would not work for me, because that would change the current URL from 'admin/foo/1/edit' to 'foo/1/edit'. I would need the 'admin' part in the URL to determine if its a normal user or admin user.aayush shresthaNov 29 '13 at 8:57
  
What I want is to be able to get the controller action name of the rest of the URL and then call that controller.aayush shresthaNov 29 '13 at 8:59
  
This is not redirect, read the uri segment using this method and then use that segment value to decide which layout you need to load.Anil SainiNov 29 '13 at 8:59

You can useRESTful Controller

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:

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');

});
shareimprove this answer
 
  
This would be one way of achieving what I want. But that is not the point. I was wondering if the name of a specific controller action can be obtained from dynamic URLs and then call that controller no matter what we get in URL.aayush shresthaNov 29 '13 at 8:55

You can useRequest::segment(index)to get part/segment of theurl

// 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
shareimprove this answer
 

来自 https://stackoverflow.com/questions/20281420/get-controller-action-from-url-laravel