10

I'm using the Laravel PHP framework and am wondering about a few things. The default application/routes.php file contains this:

Route::get('/', function()
{
    return View::make('home.index');
});

This just outputs the view, but how do I call a controller from there?

I can delete the entire route above and replace it with Route::controller('home') which seems to use the home controller on the default URL (i.e. example.com/). But any other controller like Route::controller('article') doesn't work, only on example.com/article. How would I set the article controller as the default?

tereško
  • 58,060
  • 25
  • 98
  • 150
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290

2 Answers2

19

Just pass the controller as a string, with @ between the class name and the method name:

Route::get('/', 'article@index');

Read the docs (scroll to the example by the title Registering a route that points to a controller action).

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Thanks, somehow I missed that part of the page. What about the `home` thing though? Is `Route::get('/', 'home@index')` the same as `Route::controller('home')` ? – DisgruntledGoat Jan 18 '13 at 17:08
  • @DisgruntledGoat - Nope. `Route::get('/', 'home@index')` will only register the index method, and only on the `/` route. `Route::controller('home')` will register all its methods, mapped to their respective URLs. – Joseph Silber Jan 18 '13 at 17:47
  • How would the latter work? If you register an entire controller to `/` then doesn't `/example` become ambiguous, between a controller called 'example' vs an 'example' method on the home controller. – DisgruntledGoat Jan 18 '13 at 18:07
  • You can't register an entire controller to `/`. To register a route, you register one specific method. If you want to register an entire controller, you use `Route::controller('controller_name')`. – Joseph Silber Jan 18 '13 at 18:10
  • What I'm saying is, if you register the home controller using `Route::controller('home')`, it becomes the default controller on `/` (unless `/` is already specified). Or at least, the index method of the home controller is registered. – DisgruntledGoat Jan 18 '13 at 18:14
  • @DisgruntledGoat - I'm not following you. What is the specific problem that you're trying to solve (or understand)? – Joseph Silber Jan 18 '13 at 18:15
  • I'm just getting confused about the home controller. If you have `Route::controller('home')` and nothing else in your routes file, the home controller becomes the default and is available on the root URL (`/`) as well as the expected `home/`. However, that's not the case if you just put `Route::controller('article')` and nothing else, it is only available on `/article`. (P.S. don't worry, you've answered my main question.) – DisgruntledGoat Jan 18 '13 at 18:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22986/discussion-between-joseph-silber-and-disgruntledgoat) – Joseph Silber Jan 18 '13 at 18:26
  • @DisgruntledGoat Read the Laravel documentation on routing and controllers. Throughly. The questions you’re asking are answered in there. – Martin Bean Jan 10 '18 at 11:36
  • @Martin This is a 5 year old question. It was asked at a time when the docs were not as good as they are now (Laravel 4 or even L3). And my question from the comments is *not* answered there any more since Laravel removed controller routes. – DisgruntledGoat Jan 10 '18 at 16:38
2

The "/" is special location and you can set it via Route::get('/','home@index').

For all other actions on home controller you will have urls such has "/home/action1" or "/home/action2".

I am just trying to make you understand that there is no benefit and need of making any controller assign to "/".

I hope I am clear with my reply. Again this is not an attempted answer to your question but a suggestion for you if you are stuck with route handling. I was at the same stage where you are few days back :)

Etibar
  • 578
  • 6
  • 22
Naveen
  • 1,067
  • 2
  • 14
  • 36
  • 2
    This is an old post but it's one of the top in Google so I am commenting for that reason. If anyone finds this, when you set a Route::controller('/', 'Controller') you break all other links which is why it is not a good idea to use the default '/' for a controller route – Patrick Cauley Mar 18 '14 at 20:32