I have following route definition in my web.php:
Route::get('/captcha','Api\MyCaptchaController#captcha'); // this will return a service entry url: /captcha/default
Route::get('/{cslug1}/{cslug2}','Front\FrontController#default_index');
and in the CaptachaServiceProvider boot() method, we defined the /captcha/default route:
$this->app['router']->get('captcha/default', '\Mews\Captcha\CaptchaController#getCaptcha')
and above route the action which will serve /captcha/default url request.
The issue is: When above url request is lauched by browser, the default wildcast route entity is hit.(/{cslug1}/{cslug2})
How to make sure the captcha/default is served by CaptchaController instead of FrontController?
Laravel will match the first route it finds. So if /{cslug1}/{cslug2} is registered with the router before your captcha/default route, then that will be invoked and the router will stop looking for any other matches.
Instead, you’ll need to make sure your wildcard route is registered after all other routes. You can do this by putting it in its own service provider and making sure it’s the last one registered in your config/app file:
protected $providers = [
// All other providers...
// Add service provider here that registers /{cslug1}/{cslug2} route
];
Define your FrontController route to ignore requests if first parameter is captcha:
Route::get('/{cslug1}/{cslug2}','Front\FrontController#default_index')
->where('cslug1', '^((?!captcha).)*$');
Related
The API endpoint /clients/entries returns a 404 error while using the route setup.
Route::apiResource('clients', ClientController::class);
Route::get('clients/entries', [ClientController::class , 'getAll']);
The endpoint only works when they are rearranged, so the resource route is at the end.
Route::get('clients/entries', [ClientController::class , 'getAll']);
Route::apiResource('clients', ClientController::class);
Why does this issue occur? And is it fine to have the resource route at the end?
A full explanation can be found at https://stackoverflow.com/a/62952620/9004987 (thanks to #Espresso).
Summary:
When the resource route is registered at the beginning it will create some routes. Example:
GET /clients/{client} show clients.show
And when other custom routes (such as /clients/entries) are registered after the resource route then it will conflict with the resource URI (since it has the same pattern).
Solution:
Define the custom routes before the resource route.
Firs:
You need to direct requests from Routes/web.php to API
Route::get('/{any}', [App\Http\Controllers\ClientController::class, 'index'])->where('any','.*');
Requests from routes/api.php need to be answered
im using a package called https://github.com/garygreen/pretty-routes
there is line in its service provider boot() method (here the code)
it is defining a get route with middlewares from its config file(link to the code) I just added 'auth:web' to its config file but it seems the 'auth:web' middleware is called as soon as code reaches the line before Laravel bootstraps its session and etc. when the auth('web')->user() is yet null
What I can not understand is that I do the same exact thing (here the code)with laravel/telescope but it works. why ???
also changing :
Route::get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
to :
$this->app['router']->get(config('pretty-routes.url'), 'PrettyRoutes\PrettyRoutesController#show')
->name('pretty-routes.show')
->middleware(config('pretty-routes.middlewares'));
in service provider seems to solve the problem and make this code behave like the way telescope package use 'auth:web' as middleware.
what's happening ?
You need to have the web middleware applied to any routes you need sessions for, which is what the default authentication system is using. When you apply the auth middleware without this it can't possibly resolve a user since there is no session to be authenticated against.
You need to apply the web middleware and then what ever other middleware you want:
'middlewares' => [
'web', 'auth:web',
],
If you look at the telescope example you provided you will see they also add the web middleware. So you didn't quite "do the same exact thing" as the telescope config.
Structury of folders:
-App
-CustomClasses
Menu.php
-Http
-Controllers
TestController.php
When i use Route::getRoutes() from my TestController.php i've got all routes, but when i use same method in Menu.php result is empty. Therefore route('route-name') which i need also doesn't work correctly. I think it might be because of different namespaces, but is there any simple way to get route url by route name in Menu.php or any other file outside Controllers folder?
After some time i found solution myself. In app/Providers/AppServiceProvider.php there are two methods: register() and boot(). I created instance of Menu object in boot() method, but still received empty route list. I notticed that in config/app.php there is a list of service providers, but order is important here.
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
As you can see AppSeriveProvider is before RouteServiceProvider which means Route's boot method is run last, so there are no routes yet if i try to get them in Menu object which is creater in AppServiceProvider.
My solution is to create MenuServiceProvider and load it after Route's provider instead create it in AppServiceProvider.
I am making a module for Laravel, and for the ease of reusability, I am making it as a separate composer module.
In this module, I have to define a catch-all route, but I dont want it to override any of the manualy added routes, in the project.
Does anyone have a good idea how I can get this behaviour?
I am registering my route in the ServiceProviders boot() method like this:
public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/migrations');
$this->loadRoutesFrom(__DIR__.'/routes/routes.php');
}
and the routes.php is also rather simple:
$regex = ".*";
Route::namespace('Asator\\Runepost\\Controllers')
->middleware(['web', DynamicContent::class])
->group(function($route) use ($regex) {
$route->any('{any}', 'RunepostFrontController')->where('any', $regex);
});
Is it possible to somehow add the route as the last route, after the manually added routes has run?
Just add your module's ServiceProvider after the App's RouteServiceProvider in config/app.php and make sure your catch-all-route is the last route of your module's routes.
Laravel 5.5
which is the different doing (no get and post methods) on route defintion web.php file:
$this->get('login', 'Auth\LoginController#showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController#login');
regarding ->name('') method
Is required define that method? in which cases?
(sample taked from Auth Class definition laravel)
The idea of defining ->name() in routes is for easier code maintenance in the future, it's not mandatory.
Say for example you have few places which uses the route login, one fine day you update the route to user-login. You will have to find and update all the route being used, changing from url('login') to url('user-login').
If you have a route name defined, you will be using route('login'), when you update your route url, there's no need to update all the other files that you're using that route.