Laravel - Strange happenings on routes - laravel-5

I have searched online but nothing seems to match my issue. I have a little idea of what it may be related too, but not sure. I have a basic User CRUD. For my routes, I am adding them in web.app but trying to prefix them with api/
When I list my routes, I can see the correct routes, here is an example of the index
| GET|HEAD | api/user | user.index | App\Http\Controllers\UserController#index | web,auth
So within web.php I have a basic resource
Route::resource('api/user', 'UserController');
Now when I hit this route, it doesnt seem to work. So I thought it may be to do with the fact that the api.php file has an api prefix. So within this file, I added
Route::get('/user', 'UserController#index');
And now it works. This is where it is weird though. If I remove the initial resource from web.php, I get an authentication error. To have it work, I need the resource within web.php and the get in api.php.
However, I can make POST calls directly to the User Controller directly within web.php
Route::post('api/user', 'UserController#store');
This works without the need to add anything within api.php. This is where it gets weird though. I thought it must be the api prefix, although this does not really explain why I need it in both route files. So within web.php, I changed it to
Route::resource('test/user', 'UserController');
But once again, without anything within api.php, the GET does not work but the POST does.
Does anyone have any idea of what is going on?
Thanks

Related

How to avoid overwriting routes in Laravel?

Sorry in advance, I know it has been asked before but I did not figure out the solution.
I am new to Laravel, still learning and stuck with this issue:
My objective is to add pages in admin and show these pages in frontend.
For the Front part of the website I have this route:
Route::get('/{page}', 'PagesController#show');
so the when you access /about, /contact, /another-page I use the same view
For the Admin part of the website I have this route:
Route::get('/admin', 'AdminController#show');
My problem is that the first route overwrites the second route and I don't know how to avoid this.
I have tried with namespaces and grouping routes, but I get the same outcome.
Thank you
To make it simple this is happening because you have the route with the parameter before the admin route so is going to send the "admin as a parameter of page"
The Simple fix is just put admin route before your "/{page} so it will find admin route first,Something Like this:
Route::get('/admin', 'AdminController#show');
Route::get('/{page}', 'PagesController#show');
But I do not recommend building your routes this way and have specifics pages setup if possible, This way of building routes will mess around with the 404 route not found aswell.

Routing via Laravel API

I have a very small software and I use laravel and vue.js. I would like to know the difference between routing though api.php and web.php in routes folder. Can somebody explain me the difference in these two cases?
You can define the routes in either web.php or api.php. However, routes in api.php have certain advantages
routes get automatically prefixed with '/api/'
routes use api-middleware and auth. This can add certain additional security by throttling API-requests.
You should use api.php for all your api needs or any routes that will be called through ajax.
:

URL Routing: Laravel

I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.

How to find out what middlewares are used?

I've inherited a codebase that I'm trying to prune - looking at middlewares I see some that I can't see being used in routes, nor controllers, but I'm worried that I'm not taking into account all scenarios.
For example - there's this middleware: '2fa' => \PragmaRX\Google2FALaravel\Middleware::class that I can't see being used anywhere, yet when I comment it, our 2fa functionality fails to work.
As such, the question is - is there a way to see where given middlewares are used?
PS. I'm talking here purely about middlewares references within $routeMiddleware array
This will give you a list of all the routes in your application, with the middleware applied
Go to your project root and write in
php artisan route:list
if it's too many records, use grep
php artisan route:list | grep "fa"
if It's not being applied to the routes file, try viewing in the controllers or the RouteServiceProvider
Edit:
If your project is using its facade, you can try and search for use Google2FA in your project

Laravel 5.3 session cookies are not creating in browser

I am facing token mismatch issue in new server while working fine in localhost. I have tried every possible way to clear cache and give SESSION_DOMAIN path in env but all seems useless.
Also session cookies are not being created in web browser while creating in storage/framework/session folder.
Please help me !
Are you getting tokenMismatchException exception?
If yes, some of the possible reasons are:
Check your files for PHP end tag "?>", if exists remove it. For more detail refer to this link.
You may need to use web middleware. For more detail refer to this link (although it is about laravel 5.2 but, it may work for your situation too).
Another thing to try is checking for web middleware presence. Normally it should be automatically applied to routes/web.php
Route::group(['middleware' => ['web']], function () {
<routes>
});
Also check out https://laravel.com/docs/5.3/upgrade to see if you have any code that might have been influenced by this update.
And lastly, it would be nice if you could post a piece of code which is responsible for sessions in your app.

Resources