Laravel 控制器不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18643016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Controller not working
提问by Lance
I'm very new to the Laravel framework and am trying to load a simple controller in my browser to slowly get the hang of things.
我对 Laravel 框架非常陌生,我正在尝试在我的浏览器中加载一个简单的控制器以慢慢掌握一切。
I have a file that's titled users.php inside of the the laravel/app/controllers/ folder and it looks like this:
我在 laravel/app/controllers/ 文件夹中有一个名为 users.php 的文件,它看起来像这样:
class UsersController extends BaseController
{
public $restful = true;
public function action_index()
{
echo 'hi';
}
}
In the routes.php file, I have
在 routes.php 文件中,我有
Route::get('users', 'UsersController@index');
But, when I go to
但是,当我去
http://localhost:8888/laravel/public/users
I'm greeted with a message that says "ReflectionException Class UsersController does not exist"
我收到一条消息,上面写着“ReflectionException Class UsersController 不存在”
I'm not sure if this is because I didn't install the mcrypt extension of PHP. But, when I checked the php.ini file on MAMP, it said that it was enabled. Upon entering
我不确定这是不是因为我没有安装 PHP 的 mcrypt 扩展。但是,当我检查 MAMP 上的 php.ini 文件时,它说它已启用。输入时
which PHP
in my terminal, it said /usr/bin/php. So, it might not be using the correct version of PHP.
在我的终端中,它说 /usr/bin/php。因此,它可能没有使用正确版本的 PHP。
I'm not entirely sure if this is a routes problem or if it's stemming from an absence of a vital PHP extension.
我不完全确定这是路由问题还是由于缺少重要的 PHP 扩展。
Thanks a bunch!
谢谢一堆!
回答by msturdy
You need to use the Route::controller method to reference your Controller:
您需要使用 Route::controller 方法来引用您的控制器:
Route::controller('test', 'TestController');
...and rename your file (as Cryode mentions ) to be TestController.php
.
...并将您的文件(如 Cryode 提到的)重命名为TestController.php
.
Note - if you want to use the filename as test.php, then you will need to use composerto update the autoload settings.
注意 - 如果您想使用文件名作为 test.php,那么您将需要使用composer来更新自动加载设置。
Finally, the format of names for Controller methods changed in Laravel 4, try renaming the method
最后,Laravel 4 中控制器方法的名称格式发生了变化,尝试重命名该方法
public function action_index() {}
to be
成为
public function getIndex() {}
the get
represents a HTTP GET request... the same applies for post
(HTTP POST) and any
(GET or POST.. )
的get
表示HTTP GET请求......同样适用于post
(HTTP POST)和any
(GET或POST ..)
回答by Aken Roberts
I'm not familiar with that part of Laravel's source, so I'm not entirely certain that this is the issue, but your controller file name should match the controller class name, including capitalization.
我不熟悉 Laravel 源代码的那部分,所以我不完全确定这是问题所在,但您的控制器文件名应该与控制器类名匹配,包括大写。
So users.php
should be UsersController.php
. Now, when I do this myself on purpose, I get a "No such file or directory" error on an include()
call, so that's why I'm not certain that's the sole cause of your problem. But it may be a start.
所以users.php
应该是UsersController.php
。现在,当我故意这样做时,我在include()
通话中收到“没有这样的文件或目录”错误,所以这就是为什么我不确定这是您问题的唯一原因。但这可能是一个开始。