laravel 4 中的路由问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14493902/
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
Routes issue in laravel 4
提问by Om3ga
I am new to laravel and learning it now. I am giving following Route in routes.php
file
我是 laravel 的新手,现在正在学习。我在routes.php
文件中给出以下路线
Route::resource('contacts', 'ContactsController');
But when I load my page in browser, it gives me following error
但是当我在浏览器中加载我的页面时,它给了我以下错误
Unhandled Exception
Message:
Call to undefined method Laravel\Routing\Route::resource()
Location:
/Users/zafarsaleem/Sites/learning-laravel/application/routes.php on line 35
My complete routes.php file is below
我完整的 routes.php 文件如下
Route::resource('contacts', 'ContactsController');
Route::get('/', function() //<------- This is line 35
{
return View::make('home.index');
});
How can I remove this error?
我怎样才能消除这个错误?
Edit
编辑
ContactsController code is below and I want index() function to be used
ContactsController 代码在下面,我想使用 index() 函数
class ContactsController extends BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
Contact::all();
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$input = Input::json();
Contact::create(array(
'first_name' => $input->first_name
'last_name' => $input->last_name
'email_address' => $input->email_address
'description' => $input->description
));
}
/**
* Display the specified resource.
*
* @return Response
*/
public function show($id)
{
return Contact::find($id);
}
/**
* Show the form for editing the specified resource.
*
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @return Response
*/
public function update($id)
{
$contact = Contact::find($id);
$input = Input::json();
$contact->first_name = $input->first_name;
$contact->last_name = $input->last_name;
$contact->email_address = $input->email_ddress;
$contact->description = $input->description;
$contact->save();
}
/**
* Remove the specified resource from storage.
*
* @return Response
*/
public function destroy($id)
{
return Contact::find($id)->delete();
}
}
Edit 2
编辑 2
I tried both following routes but ended up same below error
我尝试了以下两条路线,但最终出现以下错误
Route::resource('contacts', 'ContactsController', ['only', => ['index']]);
Route::get('contacts','ContactsController@index');
After reinstalling laravel 4 now I am getting following error
现在重新安装 laravel 4 后,出现以下错误
404 Not Found
The requested URL /contacts was not found on this server.
_____________________________________________________________________
Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch Server at bb.dev Port 80
Edit 3
编辑 3
Here is what did now, I edit "/private/etc/apache2/users/.conf" and changed from "AllowOverride None" to "AllowOverride All" and then restarted my apache server. Now I am getting following error
这是现在所做的,我编辑“/private/etc/apache2/users/.conf”并将“AllowOverride None”更改为“AllowOverride All”,然后重新启动我的apache服务器。现在我收到以下错误
403 Forbidden
You don't have permission to access /contacts on this server.
__________________________________________________________________________________
Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch Server at bb.dev Port 80
Why don't I have permission for this contacts controller? It is making me crazy now.
为什么我没有此联系人控制器的权限?现在让我发疯了。
Here is my .htaccess file
这是我的 .htaccess 文件
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
回答by spacek33z
Have you tried this on another server? A lot of things can go wrong with rewriting (I've lost hours fixing the .htaccess), so the problem may be Apache and not Laravel.
你在另一台服务器上试过这个吗?重写时可能会出现很多问题(我花了几个小时来修复 .htaccess),所以问题可能出在 Apache 而不是 Laravel。
This works for me in public/.htaccess
:
这对我有用public/.htaccess
:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]
</IfModule>
And have you tried going to index.php/contacts
instead of /contacts
? If that works, the problem is Apache, not Laravel.
你有没有试过去index.php/contacts
而不是/contacts
?如果可行,则问题出在 Apache,而不是 Laravel。
回答by J.T. Grimes
It may be a namespace issue -- it should be calling the function on Illuminate\Routing\Router but your exception refers to Laravel\Routing\Route::resource().
这可能是命名空间问题——它应该调用Illuminate\Routing\Router上的函数,但您的异常指的是Laravel\Routing\Route::resource()。
Is it possible that your configuration file still has references to Laravel3 in it?
您的配置文件中是否仍有对 Laravel3 的引用?
回答by mogetutu
Try changing your routes to
尝试将您的路线更改为
Route::resource('/contacts', 'ContactsController');
In ContactsController.php change index to return instance of the model
在 ContactsController.php 更改索引以返回模型的实例
public function index()
{
return Contact::all();
}
回答by Luisinho Rojas
I had the same $#%& issue, and after hours of searching I found it is not a problem of the .httacess file. What I just did to fix this:
我有同样的 $#%& 问题,经过数小时的搜索,我发现这不是 .httacess 文件的问题。我刚刚做了什么来解决这个问题:
composer update --dev
I think that the --dev
bit is the important thing. Hope that helps someone.
我认为这--dev
一点很重要。希望能帮助某人。
回答by amin
ok i had the issue up to one minute ago! it is because of ide-helper to solve you should comment the following code in routes.php
好的,我在一分钟前遇到了这个问题!这是因为ide-helper来解决你应该在routes.php中注释以下代码
use Illuminate\Routing\Route;