问题描述
我是laravel的新手,正在尽最大努力尝试RTM,但是很难理解一些事情.我认为在路由方面我尚不了解上下文的预期水平.在查看路由文档时,我发现uses
关键字允许一个Attach(ing) A Filter To A Controller Action
,但这是什么意思?我有一个使用uses
关键字的现有站点,但是我对它的实际操作感到困惑.有人可以解释一下(比laravel文档更全面的选项卡)并显示一个非常简单的示例此操作的实际作用吗?
I'm new to laravel and trying my best to RTM but having difficulty understanding a few things. I think there is an expected level on context that I am not aware of when it comes to routing. In reviewing the documentation for routing I see that the uses
keyword allows one to Attach(ing) A Filter To A Controller Action
, but what does that mean? I have an existing site that is using the uses
keyword but I am at a loss at to what it is actually doing. Can someone explain (a tab more thoroughly than the laravel documentation does) and show a very simple example what this actually does?
推荐答案
路由的关键字uses
是您定义将用于处理该特定路由的操作(控制器方法或匿名函数)的位置.以以下控制器方法示例为例:
The keyword uses
of routing is where you define which action (controller method or anonymous function) will be used to process that particular route. Take this controller method example:
Route::get('user', array('uses' => 'UserController@showProfile'));
它表示uses
将在您的UserController
类中调用方法showProfile
,这将是该类:
It says that uses
will call the method showProfile
in your UserController
class, this would be the class:
class UserController extends Controller {
public function showProfile
{
return "Hi! I'm the showProfile method!";
}
}
因此,如果您点击
http://localhost/user
您应该看到消息
Hi! I'm the showProfile method!
因为您的路线执行了您在uses
中定义的操作.
Because your route execute that action you defined in the uses
.
一个匿名函数(关闭)示例为:
An anonymous function (closure) example would be:
Route::get('user', array('uses' => function() {
return "Hi, I'm a closure!";
}));
这篇关于关于laravel路由,"uses"关键字是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!