Вопрос

I have the following code in my app/routes.php file:

<?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);
});

All my routes work fine, except for data/create. When I visit data/create, I get a 404 Not Found error. Even if I define the route as follows:

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

I still get a 404 error. However, the following works just fine:

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

I have no idea what the problem could be. I'm following along with the examples in the book "Getting Started with Laravel 4" by Raphal Saunier and the author writes code identical to what I have above (though using "cats" instead of "data").

Это было полезно?

Решение

This route:

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

is catching your hits to

/data/create

Move it to the end of your routes. Anyway it should not give you a 404...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top