Skip to content
Advertisement

Best way to obtain prefix and last part of url in Laravel

I have a group route like this

PHP LARAVEL 9

JavaScript

In the controller side I want to obtain the accomodations part and the italy greece part without slashes. I’m doing like this

PHP LARAVEL 9

JavaScript

This works but the way I’m manipulating the request in $listType seems a little bloated to me, is there a cleaner way in laravel?

Advertisement

Answer

Use parameterized route for same method.

Routes

JavaScript

Controller

JavaScript

If your destination is optional you can use optional parameter

Edit 1:

Route with Optional Parameter

JavaScript

Controller with Optional parameter

JavaScript

you can assign any value (null, string, int, etc.) to the destination variable just like the default function argument in PHP.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement