The Laravel route directives actually do accept a string as the second parameter:
Route::get('/user', 'UserController@index');
This is parsed by the Laravel router to call the desired action in the desired controller, and then inject whatever the action controller returns into the actual response to the user.
However, action controllers can return quite a few different things: views, redirects, strings, json, and so on.
If you were to just put a simple string intended to be a view name, the router will need to know that the string is a view name (and not a controller@action
, or a redirect, or a simple string...).
The closure-style callback provides flexibility - and simpler decision making in the parser.
The closure you provide in your question is rather simple - return the named view. It is possible to write some fairly complex responses:
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId)
{
// of course, you'd have to write something to get data from your models and inject into a view
});
Or even, better (thanks to implicit binding):
Route::get('posts/{post}/comments/{comment}', function (App\Post $post, App\Comment $comment)
{
return View::make('blog.comment', [
'comment' => $comment
]);
});
In order to achieve the same using strings, you'd need a more complex method footprint for the route directive.