laravel路由呼叫错误的控制器

时间:2017-12-29 17:25:27

标签: php laravel controller routes

我遇到的问题是路由引用了错误的控制器。

路线:

//Event Routes
Route::get('/event/register/thankyou', function(){return view('pages.events.thanks');})->name('event-thanks');
Route::get('/event/waitlist/confirmation', function(){return view('pages.events.waitlist-confirmation');})->name('event-waitlist-confirmation');
Route::get('/event/register/{eventId}/{price}', 'EventController@registerPage')->name('event-register-page')->middleware('auth');
Route::post('/event/register/{eventId}', 'EventController@register')->name('event-register')->middleware('auth');
Route::post('/event/waitlist/{eventId}', 'EventController@joinWaitlist')->name('event-join-waitlist')->middleware('auth');
Route::get('/event/{eventId}/{eventName?}', 'PageControllers@event')->where('eventId', '[0-9]+')->name('event');
Route::get('/regionalevents', 'EventController@regionalEvents')->name('events-regional');

在刀片模板上,我有这个按钮:

<a class="btn btn-theme-colored " href="/event/waitlist/{{ $event->id }}">Join Now!</a>

无论出于何种原因,即使在/ event / {eventId}路由中使用'where'定义器 - 当我点击按钮时,我仍然会收到此错误:

MethodNotAllowedHttpException

如果我把'where'定义器拿出来 - 我收到这个错误:

ErrorException
Trying to get property of non-object at PageControllers->event('waitlist', '1002')

本质上,它调用PageControllers上的事件函数而不是EventController上的joinWaitlist函数。

我尝试重新排序路线,但它也没有用。

在PageControllers事件函数中,我放了一个'dd($ eventId);'当这是URL时:http://localhost:8000/event/waitlist/1002 - 它返回“waitlist”

我有什么东西直接失踪吗?我完全陷入了困境。任何帮助深表感谢!

2 个答案:

答案 0 :(得分:2)

您正在尝试路由/event/waitlist/{eventId},这是锚标记中的POST方法,它发送GET方法。而不是在刀片中调用静态URL,使用如下名称:

Route::get('/event/waitlist/{eventId}', 'EventController@joinWaitlist')->name('event-join-waitlist')->middleware('auth');
刀片中的

<a class="btn btn-theme-colored " href="{{ route('event-join-waitlist', [$event->id]) }}">Join Now!</a>

答案 1 :(得分:1)

您需要将其更改为Route::get('/event/waitlist/{eventId}' 才能使其正常工作:

Route::post('/event/waitlist/{eventId}'

而不是:

a href

因为您在使用{{1}}链接时发送了GET请求。