这是EventController: <?php命名空间App \ Http \ Controllers;使用Illuminate \ Support \ Facades \ Input;使用Illuminate \ Http \ Request;使用App \ EventModel;EventController类扩展Controller{公共功能index(){$ events = EventModel :: all();返回\ View :: make('event/index')-> with('events',$ events);}公共功能视图($ id){返回\ View :: make('event/view')-> with('event',EventModel :: find($ id));}公共函数newEvent(){dd("dd");返回\ View :: make('event/create');}公共功能create(){$ validator = EventModel :: validate(Input :: all());if($ validator-> fails()){$ messages = $ validator-> messages();返回redirect()-> action('EventController @ newEvent')-> withErrors($ validator)-> withinput();}别的{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()->覆盖(已创建新事件!",成功");返回redirect()-> back();}}公共功能edit($ id){返回\ View :: make('event/edit')-> with('event',EventModel :: find($ id));}公共功能update(){$ event = EventModel :: find(Input :: get('event_id'));$ validator = EventModel :: validate(Input :: all());if($ validator-> fails()){$ messages = $ validator-> messages();返回redirect()-> back()-> withErrors($ validator)-> withinput();}别的{$ event-> title = Input :: get('title');$ event-> start =输入:: get('start');$ event-> end =输入:: get('end');$ event-> save();//Session :: flash('message','成功更新!');flash()->覆盖(事件已成功更新!",成功");返回redirect()-> back();}}公共函数destroy(){$ id =输入:: get('event_id');dd("$ id");}} 为什么会出现此问题?解决方案您必须对路由进行排序,因为laravel会检查路由的顺序.尝试: Route :: get('events',array('as'=>'events','uses'=>'EventController @ index'));;路线:: 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'));;路由:: delete('event/delete',array('uses'=>'EventController @ destroy'));;路线:: get('event/{id}',array('as'=>'event','uses'=>'EventController @ view')));路由:: get('event/{id}/edit',array('as'=>'edit_event','uses'=>'EventController @ edit')));; 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 [email protected] is the EventController:<?phpnamespace 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? 解决方案 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')); 这篇关于Laravel路线去错了路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 21:04