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?

2
  • How did you created routes for these ? Can you post the routes related to these ? Commented Nov 29, 2013 at 8:56
  • Can you show the routes for these ? Commented Nov 29, 2013 at 9:04

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Use this in your controller function -

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

7 Comments

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.
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.
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.
What I want is to be able to get the controller action name of the rest of the URL and then call that controller.
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.
|
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');

});

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.