【问题标题】:Routes issue in laravel 4laravel 4中的路由问题
【发布时间】: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>

【问题讨论】:

  • 可以获取、发布、放置路线......只需将其更改为适当的请求即可。另外阅读有关路由的文档。
  • @itachi 在此link 上的路线文档并没有说必须获取、发布、放置等路线。如果您在此link 上阅读 cmets,您会发现我我也可以使用。
  • 据我所知,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


【解决方案1】:

这可能是一个命名空间问题——它应该调用 Illuminate\Routing\Router 上的函数,但您的异常是指 Laravel\Routing\Route::resource ()。

您的配置文件中是否仍有对 Laravel3 的引用?

【讨论】:

  • 不,我以前从未使用过 laravel。这是我第一次使用它并从laravel 4开始。我过去没有安装过laravel 3
  • 我按照 youtube 上的一个截屏视频再次重新安装了整个 laravel。但这次它说Not Found The requested URL /contacts was not found on this server.
【解决方案2】:

您是否在另一台服务器上尝试过此操作?重写可能会出现很多问题(我已经浪费了几个小时来修复 .htaccess),所以问题可能出在 Apache 而不是 Laravel。

这对我有用 public/.htaccess:

<IfModule mod_rewrite.c>
     RewriteEngine on

     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d

     RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

您是否尝试过使用index.php/contacts 而不是/contacts?如果可行,问题出在 Apache,而不是 Laravel。

【讨论】:

  • 你拯救了我的一天!谢谢:)
【解决方案3】:

尝试将路线更改为

Route::resource('/contacts', 'ContactsController');

在 ContactsController.php 中更改索引以返回模型实例

public function index()
{
    return Contact::all();
}

【讨论】:

    【解决方案4】:

    我遇到了同样的 $#%& 问题,经过数小时的搜索,我发现这不是 .httacess 文件的问题。我刚刚做了什么来解决这个问题:

    composer update --dev
    

    我认为--dev 位很重要。希望对某人有所帮助。

    【讨论】:

      【解决方案5】:

      好的,我在一分钟前就遇到了这个问题! 这是因为 ide-helper 要解决,您应该在 routes.php 中注释以下代码

      use Illuminate\Routing\Route;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-24
        • 2013-06-03
        • 2015-08-22
        • 1970-01-01
        • 2015-04-22
        • 2013-11-08
        • 2014-09-29
        • 2018-07-26
        相关资源
        最近更新 更多