我是 laravel 的新手,现在正在学习它。我在routes.php
文件中给出以下路线
Route::resource('contacts', 'ContactsController');
但是当我在浏览器中加载我的页面时,它给了我以下错误
Unhandled Exception
Message:
Call to undefined method Laravel\Routing\Route::resource()
Location:
/Users/zafarsaleem/Sites/learning-laravel/application/routes.php on line 35
我完整的 routes.php 文件如下
Route::resource('contacts', 'ContactsController');
Route::get('/', function() //<------- This is line 35
{
return View::make('home.index');
});
如何消除此错误?
编辑
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();
}
}
编辑 2
我尝试了以下两条路线,但最终出现相同的错误
Route::resource('contacts', 'ContactsController', ['only', => ['index']]);
Route::get('contacts','ContactsController@index');
现在重新安装 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
编辑 3
这是现在所做的,我编辑“/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
为什么我没有此联系人控制器的权限?现在让我发疯了。
这是我的 .htaccess 文件
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>