【问题标题】:Laravel gives 404 error even though route is defined即使定义了路由,Laravel 也会出现 404 错误
【发布时间】:2014-06-22 04:19:46
【问题描述】:

我的 app/routes.php 文件中有以下代码:

<?php

// Route/model binding for data
Route::model('data', 'Data');

Route::get('/', function() {
  return Redirect::to("data");
});


// Display all data (of all types)
Route::get('data', function(){
    $data = Data::all();
    return View::make('data.index')
    ->with('data', $data);
});


// Display all data of a certain type
Route::get('data/type/{name}', function($name){
    $type = Data::whereName($name)->with('data')->first();
    return View::make('data.index')
    ->with('type', $type)
    ->with('data', $type->data);
});
Route::get('data/{data}', function($data){
    return View::make('data.single')
    ->with('data', $data);
});


// Create/Add new data
Route::get('data/create', function(){
    $data = new Data;
    return View::make('data.edit')
    ->with('data', $data)
    ->with('method', 'post');
});
Route::post('data', function(){
    $data = Data::create(Input::all());
    return Redirect::to('data/'.$data->id)
    ->with('message', 'Seccessfully added data!');
});


// Edit data
Route::get('data/{data}/edit', function(Data $data){
    return View::make('data.edit')
    ->with('data', $data)
    ->with('method', 'put');
});
Route::put('data/{data}', function(){
    $data->update(Input::all());
    return Redirect::to('data/'.$data->id)
    ->with('message', 'Seccessfully updated page!');
});


// Delete data
Route::get('data/{data}/delete', function(Data $data){
    return View::make('data.edit')
    ->with('data', $data)
    ->with('method', 'delete');
});
Route::delete('data/{data}', function(Data $data){
    $data->delete();
    return Redirect::to('data')
    ->with('message', 'Seccessfully deleted data!');
});


// The about page (static)
Route::get('about', function(){
    return View::make('about');
});


// View composer
View::composer('data.edit', function($view){
    $types = Type::all();

    if(count($types) > 0)
    {
        $type_options = array_combine($types->lists('id'),
                                        $types->lists('name'));
    }
    else
    {
        $type_options = array(null, 'Unspecified');
    }

    $view->with('type_options', $type_options);
});

我所有的路线都可以正常工作,除了数据/创建。当我访问数据/创建时,我收到 404 Not Found 错误。即使我将路线定义如下:

Route::get('data/create', function(){
    return "Test";
});

我仍然收到 404 错误。但是,以下工作正常:

Route::get('somethingElse/create', function(){
    return "Test";
});

我不知道问题可能是什么。我正在遵循 Raphal Saunier 的“Laravel 4 入门”一书中的示例,作者编写的代码与我上面的代码相同(尽管使用“cats”而不是“data”)。

【问题讨论】:

    标签: laravel laravel-4 http-status-code-404


    【解决方案1】:

    这条路线:

    Route::get('data/{data}', function($data){
        return View::make('data.single')
        ->with('data', $data);
    });
    

    正在吸引您的点击

    /data/create
    

    将其移至路线的尽头。无论如何它不应该给你一个 404...

    【讨论】:

    • 确实是这个问题。
    • 它给出 404,因为 {data} 绑定到模型,而 create 不是有效的 Data 模型,因此是 404。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2017-05-02
    • 1970-01-01
    • 2014-01-25
    • 2021-01-04
    • 2021-12-02
    • 2018-12-19
    相关资源
    最近更新 更多