简体   繁体   中英

laravel route calling wrong controller

I am having an issue where a route is referencing the wrong 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');

On a blade template, I have this button:

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

For whatever reason, even with the 'where' definer in the /event/{eventId} route - I'm still getting this error when I click the button:

MethodNotAllowedHttpException

If I take the 'where' definer out - I get this error:

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

Essentially, it's calling the event function on PageControllers instead of the joinWaitlist function on the EventController.

I've tried reordering the routes, but it's not working either.

On the PageControllers event function, I put a 'dd($eventId);' and when this is the URL: http://localhost:8000/event/waitlist/1002 - it returns "waitlist"

Is there something I'm just straight up missing? I'm completely stuck at this point. Any help is much appreciated!

you are trying route /event/waitlist/{eventId} which is POST method in anchor tag, which send GET method. Also instead of calling static url in blade, use name as below:

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

in blade

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

You need to change it to get to make it work:

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

Instead of:

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

Because you're sending a GET request when you're using a href link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM