How to create a custom route?
-
Hi, I would like to create a custom route that points to a controller/action. I have tried adding the route to
routes/web.php
but it seems like that file isn't being used?I've read relevant posts on this forum but they all talk about
frontendRoutes
in modules - this hasn't anything to do with a specific module, just a basic route that I would like to handle in my own controller.Many thanks.
-
Found it - enter your custom routes to
Modules/Page/Http/frontendRoutes.php
and be sure to add them to the top of the stack!
-
Ah...but...
If i define the route:
$router->get('something/{slug}/out', [ 'uses' => 'OutController' ]);
Then laravel looks in
Modules/Page/Http/Controllers/
for the controller. Do I have to put all my custom controller in there, or is there a way to make it look in the main Controller directory i.e.app/Http/Controllers/
?
-
@pavsid Just make a custom module, always try to restrain from editing core modules, so you don't have to worry about updating project.
-
@armababy said in How to create a custom route?:
@pavsid Just make a custom module, always try to restrain from editing core modules, so you don't have to worry about updating project.
Thanks @armababy - i've created a custom module, and added my custom route the the module's
frontendRoutes.php
, but nothing appears to be happening and I am getting "page not found" for my route - is there something else I need to do? The module is enabled in the admin area.
-
Sussed it...
protected function getFrontendRoute() { return __DIR__ . '/../Http/frontendRoutes.php'; }
..in the module's
RouteServiceProvider
-
Unless you are using something strange to develop then no it should be fine as this. (For example windows lamp or anything like that).
-
@armababy lol - I am using Ampps, but seems to be fine, and have uploaded to production server and fine there too (production server is linux)
-
@pavsid So you are having this same issue when your project on a production server?
-
@armababy No issue, everything OK on both environments using the
getFrontendRoute
method above. Thanks.
-
@pavsid ah lol, sry I thought it's still not working for you. Great that it does