2014-05-05 50 views
1

我在我的應用程序/ routes.php文件文件中的以下代碼404錯誤:Laravel給人即使路由被定義

<?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入門」中的例子,作者寫的代碼與我上面的代碼相同(儘管使用「cats」代替「data」)。

回答

3

這條路線:

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

正趕上你的命中

/data/create 

它移動到你的路由結束。無論如何它不應該給你一個404 ...

+0

這確實是這個問題。 – chipit24

+0

它給出了一個404,因爲'{data}'綁定到一個模型,'create'不是一個有效的'Data'模型,因此是404。 – ollieread