How to find out what middlewares are used? - laravel

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

Related

Is it possible for the laravel web route file accessible in postman software

is it possible for the laravel web route file accessible in postman software
It's not possible out of the box. One could create a route echoing the route file that you desire, but it's not something recommended and would make your application prone to security vulnerabilities.
If, on the other hand, what you need is the list of routes that you application has available, you can use the following command:
php artisan route:list
Make sure your terminal is placed within your project folder before running the command

Laravel Class Controller not found because route is weirdly case sensitive

I wanted to add a controller to an already existing project of mine. Wrote a controller file, added the link in the admin_api route file and adjusted the view I needed.
On my local machine everything works fine.
On production it failed (it's supposed to retrieve a collection, which is then handled and displayed by a vue js component) and the network monitor showed an error 500.
I ssh into the website and ran php artisan route:list which gave me the error
In Container.php line 779:
Class App\Http\Controllers\Admin\Api\StatsController does not exist
Although the file StatsController is in App\Http\Controllers\Admin\API\
After playing around I realized that the problem was with the folder name "API", as laravel was looking for "Api". So apparently it's case sensitive.
Placing the controller in a new folder "Api" solved the issue.
Moving all the other "API" controllers to "Api" broke their routes.
when running php artisan route:list and in RouteServiceProvider all routes appear as under Admin/Api.
What I don't get is where is this being registered? Is there a way to change this?
I find it quite annoying that the controllers sit now in separate folders and it seems quite absurd to me. I don't quite get how the original controllers worked if they seem to contradict the other configurations.
Working with Laravel 5.7
I would appreciate any insights!
Thanks
Just put all the relevant api controllers in a folder named Api or API (just pick a convention and stick with it), then make sure that each file's namespace (declared at the top) is correct and reflects the folder structure you have chosen.
For example, if your controllers are in app/Http/Controllers/Admin/Api, check that each controller file begins with:
<?php
namespace App\Http\Controllers\Admin\Api;
// Rest of code...
StatsController.php must have the namespace as App\Http\Controllers\Admin\API. In Controller.php you have to import as App\Http\Controllers\Admin\API\StatsController. After that, run composer dump-autoload

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.

Route [password.request] not defined. But Auth is in routing [laravel]

My project crashes on login screen. It tries to get password.request route which it does not see. In routing, there is of course
Auth::routes();
but I did add some other stuff that uses authentication mechanisms. I copied remember password views and controllers from vendors and renamed them to reuse for other purposes.
To be honest I don't even know how to debug missing route. Any help?
You shouldn't have to copy views and controllers from the vendors folder manually if you're doing things with Laravel. Instead run php artisan make:auth in a console. This command will copy the necessary auth blade templates and update your routes/web.php to suit. There is some more info here in the official docs on Laravel authentication.
Also, if you haven't already, you'll need to run migrations by running php artisan migrate in a console. This will create the users and password_resets tables needed to allow registration, login and password recovery.
If you're still having trouble and want to debug things more, make sure you set APP_DEBUG=true inside your .env file in the projects root path. This should record a stack trace in storage/logs/laravel.log when an error occurs.

Missing routes.php File in New Laravel Project

I downloaded Composer, installed Laravel, and started my first Laravel project to begin learning Laravel using the lessons on laracast (great lessons). Lesson two covers routes. My new project does not have a routes.php file.
I deleted composer and started again. Same thing. Tried two different computers. Same thing. I was using NetBeans so I tried using PHP Storm. Same thing. I tried making my own routes.php file but it doesn't seem to work right because I know nothing about Laravel at this point. I tried creating and saving the project in htdocs, and then PHPStorm project folder, again - no routes.php file.
Composer is saved here- C:\Users\myName\AppData\Roaming\Composer\vendor\bin. I used composer global require "laravel/installer" in command prompt to install laravel. Any ideas?
The lastest version of Laravel doesn't have a routes.php file.
This 'routes.php' file was located in \app\Http in the older versions.
In the newer version, Laravel 5.3, we have a folder named 'routes', where we can find the following files:
api.php
console.php
web.php
For this new version, the routes for your controllers, you can put inside web.php file
See the documentation about routing here
https://laravel.com/docs/5.3/routing#basic-routing
The video lesson you are watching may be outdated.
In the Latest Laravel they have removed common routes.php where as they have added different route files to better manage your application routes.
There is
routes/web.php : routes file which works similar to routes.php file where you can have your routes and all the POST routes in web.php file will be validated for the CSRF Token similar to normal Laravel Post route.
routes/api.php : routes file where you can have your Application's API routes, the URL will be example.com/api/ Eg. If you have route getUsers then the API URL will be example.com/api/getUsers. The most important thing to notice is POST requests to an API url will not be validated for CSRF Token.
routes/console.php : routes file where you can define your Artisan commands which you can run from Laravel Artisan CLI.
Laravel new version don't have routes.php
It has
1.web.php
To create Web routes
2.api.php
if you are using front (js) framework then write routes here
3.console.php
the console.php used for console commands and interaction with commands
#Geraldo has answered it well but still something more you can learn-
In Laravel newer version, old types of routes.php file has deleted.
Why removed:
From Laravel announcement, it has done to give more flexibility to the routes.
Solution:
Now there, a route folder has added and inside that folder there are 4 files.
web.php -- The previous routes were in this files mainly. Here is where you can register web routes for your application.
api.php -- Here is where you can register API routes for your application.
channels.php -- Here you may register all of the event broadcasting channels that your application supports.
console.php -- For all of the console commands and interaction with commands.
See, now it is more flexible for you to add any API and then link it via it's api.php route file and normal route in web.php file. Thanks.
In version 5.6 there is no routes.php file under Http/Requests, from the docs:
All Laravel routes are defined in your route files, which are located
in the routes directory. These files are automatically loaded by the
framework. The routes/web.php file defines routes that are for your
web interface. These routes are assigned the web middleware group,
which provides features like session state and CSRF protection. The
routes in routes/api.php are stateless and are assigned the api
middleware group.
For most applications, you will begin by defining routes in your
routes/web.php file. The routes defined in routes/web.php may be
accessed by entering the defined route's URL in your browser. For
example, you may access the following route by navigating to
http://your-app.test/user in your browser:
Route::get('/user', 'UserController#index');
Listen
Go to
Project Folder Name --> app --> Http --> routes.php
You will find routes there.
In the newer version,
Laravel 5.3, find folder named 'routes', where following files are present:
api.php
console.php
web.php
For this new version, the routes for your controllers, you can write in web.php file
Laravel new version doesn't have routes.php
1.web.php To create Web routes
2.api.php if you are using front (js) framework then write routes here
3.console.php the console.php used for console commands and interaction with commands

Resources