1

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 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,

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
aayush shrestha
  • 1,858
  • 2
  • 17
  • 33

4 Answers4

4

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.

Community
  • 1
  • 1
aayush shrestha
  • 1,858
  • 2
  • 17
  • 33
1

You can use Request::segment(index) to get part/segment of the 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
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

You can use RESTful 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');

});
egig
  • 4,370
  • 5
  • 29
  • 50
  • 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 shrestha Nov 29 '13 at 08:55
0

Use this in your controller function -

if (Request::is('admin/*'))
{
    //layout for admin (layout 2)
}else{
    //normal layout (layout 1)
}
Anil Saini
  • 627
  • 3
  • 17
  • 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 shrestha Nov 29 '13 at 08: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 Saini Nov 29 '13 at 08: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 shrestha Nov 29 '13 at 08: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 shrestha Nov 29 '13 at 08: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 Saini Nov 29 '13 at 08:59
  • check this - [automated routing](http://stackoverflow.com/questions/18178023/laravel-4-route-to-localhost-controller-action) – Anil Saini Nov 29 '13 at 09:09
  • I just found the exact solution that i was looking for. I have posted an answer myself. Please take a look. It might prove useful to everyone. And thanks of participating. :) – aayush shrestha Nov 29 '13 at 10:35