Laravel routes going to wrong route

2019-09-15 16:22发布

问题:

I do not understand the following problem.

Here are me routes:

Route::get('events', array('as' => 'events' ,'uses' => 'EventController@index'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => 'EventController@view'));
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => 'EventController@newEvent'));
Route::post('event/create', array('uses' => 'EventController@create'));
Route::get('event/{id}/edit', array('as' => 'edit_event', 'uses' => 'EventController@edit'));
Route::post('event/update', array('uses' => 'EventController@update'));
Route::delete('event/delete', array('uses' => 'EventController@destroy'));

I can not create a new event, because when I click on the 'New Event' button, it uses EventController@view instead of EventController@newEvent.

Here is the EventController:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use App\EventModel;

class EventController extends Controller
{
    public function index()
    {
        $events = EventModel::all();
        return \View::make('event/index')->with('events', $events);
    }

    public function view($id)
    {
        return \View::make('event/view')
        ->with('event', EventModel::find($id));

    }

    public function newEvent()
    {
        dd("dd");
      return \View::make('event/create');
    }

    public function create()
    {
        $validator = EventModel::validate(Input::all());

        if($validator->fails())
        {   
            $messages = $validator->messages();

            return redirect()->action('EventController@newEvent')
            ->withErrors($validator)
            ->withinput();
        }
        else
        {
            EventModel::create(array(
                'title'=>Input::get('title'),
                'start'=>Input::get('start'),
                'end'=>Input::get('end'),
                'userID'=>\Auth::user()->id,
            ));

            //Session::flash('message', 'New event has been created!');   
            flash()->overlay('New event has been created!', 'Success');

            return redirect()->back();
        }
    }

    public function edit($id)
    {
        return \View::make('event/edit')
        ->with('event', EventModel::find($id));
    }

    public function update()
    {   
        $event = EventModel::find(Input::get('event_id'));

        $validator = EventModel::validate(Input::all());

        if($validator->fails())
        {   
            $messages = $validator->messages();

            return redirect()->back()
            ->withErrors($validator)
            ->withinput();
        }
        else
        {
            $event->title = Input::get('title');
            $event->start = Input::get('start');         
            $event->end = Input::get('end');
            $event->save();

            //Session::flash('message', 'Successfully updated!');     
            flash()->overlay('Event has been sucessfully updated!', 'Success');

            return redirect()->back();
        }
    }

    public function destroy()
    {
        $id = Input::get('event_id');
        dd("$id");
    }
}

Why does this problem occur?

回答1:

You have to sort your routes because laravel checks the order of the routes.

Try:

Route::get('events', array('as' => 'events' ,'uses' => 'EventController@index'));
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => 'EventController@newEvent'));
Route::post('event/create', array('uses' => 'EventController@create'));
Route::post('event/update', array('uses' => 'EventController@update'));
Route::delete('event/delete', array('uses' => 'EventController@destroy'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => 'EventController@view'));
Route::get('event/{id}/edit', array('as' => 'edit_event', 'uses' => 'EventController@edit'));


回答2:

Laravel route checks in the order they were defined.

event/new_event and event/{id} both have same route structure and so it is going to view action.

Change the order -

Route::get('event/new_event', array('as'=> 'new_event', 'uses' => 'EventController@newEvent'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => 'EventController@view'));