0

In Laravel the default controller is the Home_Controller. However I have a controller called frontend. I want to use this instead of the home controller.

When I register a route like this:

Route::controller(Controller::detect());

then a request to /offer will be handled from within the home controller like home@offer. I want to use frontend@offer and access it from the site's root - not like /frontend/offer.

What should I do? Thanks in advance.

Nadav S.
  • 2,429
  • 2
  • 24
  • 38
  • 2
    As an off topic, I wouldn't recommend you to use Controller::detect(). It's not recommended on Laravel 3 and completely removed from Laravel 4 since it's problematic and gives the developer little control over routes. You should use Route::controller('controller_name_here'). – vFragosop Mar 08 '13 at 15:08
  • @ViníciusFragosoPinheiro thanks for that! Can you provide a source so I can read more? – Nadav S. Mar 08 '13 at 15:23
  • [Laravel Docs](http://laravel.com/docs/routing#controller-routing) is the best place for that. Even tho it begins suggesting Controller::detect (which I hope they fix someday), there's an example a few lines below about registering home controller and multiple controllers at once. Now, about Controller::detect, there are [one](http://forums.laravel.io/viewtopic.php?id=6058#p30915) or [two](http://forums.laravel.io/viewtopic.php?id=6013#p30759) advices on forum about not using it. – vFragosop Mar 08 '13 at 17:19
  • There's a few questions about Controller::detect() like [this](http://stackoverflow.com/a/15231186/908579) and [this](http://stackoverflow.com/questions/14200516/controllerdetect-undefined-in-laravel-4) here on SO either. – vFragosop Mar 08 '13 at 17:26
  • 1
    Just dropping this one: area51.stackexchange.com/proposals/46607/laravel – Kriem Mar 09 '13 at 12:19

3 Answers3

1

Home_Controller is one of the hard-coded convention which exist in Laravel 3, however there are still ways to define routing to point the Frontend_Controller methods, my preference would be.

Route::any('/(index|offer|something)', function ($action)
{
    return Controller::call("frontend@{$action}");
});

Limitation with this is that you need to define all supported "actions" method in Frontend_Controller.

crynobone
  • 1,814
  • 12
  • 22
  • I've already tried that, but I want to change the "hard-coded" definition, as you said. I mean, the function `URL::home()` will output `frontend@index` instead of `home@index`, as well as using this controller as the DEFAULT. – Nadav S. Mar 08 '13 at 20:55
  • 1
    And - `Home_Controller` is one of the hard-coded, can I CHANGE the hard-coded configuration? – Nadav S. Mar 08 '13 at 21:15
1

My guess is that the only reason you think the Home_Controller is some sort of default is because you are using Controller::detect(); I really haven't seen anything in the documentation to make me think that the Home_Controller is anything special at all. In fact, it doesn't even look like it is routed to in the example documentation. Given that, my first suggestion would be to get rid of Controller::detect() and see if that fixes your problem.

Barring that, have you tried registering frontend as route named home? It appears that all URL::home() does is search for the 'Home' route, and then redirect to it. When using controller routing this can be done with something to the effect of.

Route::get('/',
    array(
         'as' => 'home',
         'uses' => 'frontend@index'
    )
);

Or is that not your desired effect? Do you want all routes which aren't otherwise found to be redirected to your frontend controller?

If you are concerned about your urls looking pretty, you can probably use some rewrite rules in your .htaccess file to make the whole process of routing to /frontend/index transparent you your users.

Jonathan
  • 3,369
  • 4
  • 22
  • 27
  • Brilliant. But I can't get rid of the controller prefixes, and I guess I can change it in laravel itself instead of modifying the .htaccess file – Nadav S. Mar 10 '13 at 12:07
  • What exactly do you mean the controller prefixes? – Jonathan Mar 11 '13 at 01:47
  • "make the whole process of routing to /frontend/index transparent you your users." - I mean `/frontend/{action}` will be `/{action}` – Nadav S. Mar 11 '13 at 12:04
  • Yeah, then I would recommend not bothering to do this in Laravel. Instead, I would add a rewrite rule to your .htaccess file which rewrites all of your rules from /{action} to /frontend/{action}. Putting that routing inside your Laravel introduces unecessary coupling in your application. Doing it in your .htaccess file allows you to rewrite your urls however you please in the future. – Jonathan Mar 11 '13 at 22:11
0

Add this to your routes.php :

Route::get('/', array('as' => 'any.route.name', 'uses' => 'frontend@offer'));

If you have any other / route, just remove it.

Muhammad Usman
  • 12,439
  • 6
  • 36
  • 59
  • I think you didn't understand the question. I want to register a **controller**, not an action. – Nadav S. Mar 08 '13 at 15:24
  • A Controller is a class, a method must be called `index` or whatever, isn't it :) – Muhammad Usman Mar 08 '13 at 16:38
  • See my comments on the second answer. I want to "replace" the hard-coded definition of the `home` controller, so `URL::home()` resolves to `frontend@index` – Nadav S. Mar 08 '13 at 21:24