即使定义了路由,Laravel也会给出404错误

时间:2014-05-05 18:54:44

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

我的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);
});

除数据/创建外,我的所有路线都运行良好。当我访问data / create时,我收到404 Not Found错误。即使我按如下方式定义路线:

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

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

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

我不知道问题是什么。我正在跟随Raphal Saunier所着的“Laravel 4入门”一书中的例子,作者写的代码与我上面的相同(尽管使用“猫”代替“数据”)。

1 个答案:

答案 0 :(得分:3)

这条路线:

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

正在抓住你的点击

/data/create

将其移至路线的末尾。无论如何它不应该给你404 ...

相关问题