【发布时间】:2013-01-07 18:28:28
【问题描述】:
我是 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>
【问题讨论】:
-
可以获取、发布、放置路线......只需将其更改为适当的请求即可。另外阅读有关路由的文档。
-
据我所知,laravel默认支持get、post、put、head和delete请求。如果您打开框架并查看路由是如何完成的,那么您会看到与这些请求中的每一个相关联的特定方法。如果您使用捆绑包,就会出现问题。你在用一个吗?如果是,是哪一个?
-
这里的文档中介绍了资源控制器:four.laravel.com/docs/controllers#resource-controllers。 resource() 函数在 Router 类文件的第 216 行定义。
-
我在我的 Laravel 4 上测试了你的代码 - 它工作正常。我会尝试重新安装 Laravel4,看看是否能解决它。可能是另一个问题..
标签: php routing routes laravel laravel-4