如何在使用控制器时为路由添加名称而不是在Laravel4中路由到的功能?
I guess, the one line question doesn't explain what I'm looking for well enough. So, here's an example of what I'm talking about: (based on the Laravel4 Routing documentation)
The way I create a route for the controller:
Route::get('user/profile', "UserController@profile" );
The way I create a name for the route so that I can later refer to it when generating URLs:
Route::get('user/profile', array('as' => 'profile', function()
{
//
}));
// Now I can use this
$url = URL::route('profile');
Now, obviously you wouldn't want to write your controllers as functions right in the routes.php
, so how do I create a name like in the 2nd example, but using the controller like the example 1?
我想,一行问题并不能解释我所寻找的内容。 所以,这是我正在谈论的一个例子:(基于 Laravel4路由文档) p>
我为控制器创建路径的方式: p>
Route :: get('user / profile',“UserController @ profile “);
code> pre>
我为路径创建名称的方式,以便稍后在生成URL时可以引用它: p>
Route :: get('user / profile',array('as'=>'profile',function()
{
//
}));
// 现在我可以使用这个
$ url = URL :: route('profile');
code> pre>
现在,显然你不想把你的控制器写成 函数在 routes.php code>中,所以我如何创建一个名称,如第二个例子,但使用控制器,如示例1? p>
div>
You can name your route to controller exactly the same way. Put controller and method name as second item in array under "uses" keyword:
Route::get('user/profile', array("as" => "profile", "uses" => "UserController@profile"));