In my laravel(7.x) application, I am trying to bind two routes admin/ and admin/dashboard with same name. While running the php artisan route:list command, I am getting an error that Unable to prepare route [admin/dasboard] for serialization. Another route has already been assigned name [admin.dashboard].
Web.php
Route::group([ 'prefix' => 'admin' ], function() {
...
/**
* Dashboard
*/
Route::get('/', 'Backend\DashboardController#index')->name('admin.dashboard');
Route::get('/dasboard', 'Backend\DashboardController#index')->name('admin.dashboard');
});
It was working fine in the previous versions of laravel.
How to fix this..?
You are using named routes i.e. ->name(admin.dashboard) twice but named route must be unique that is why you are getting error
Route::get('/', 'Backend\DashboardController#index')->name('admin.dashboard');
Route::get('/dasboard', 'Backend\DashboardController#index')->name('admin.dashboard');
To solve this change any one of your route to something else for e.g
Route::get('/', 'Backend\DashboardController#index')->name('admin'); // changed admin.dashboard to admin
Route::get('/dasboard', 'Backend\DashboardController#index')->name('admin.dashboard');
You can't have two routes with the same names.
Route::group([ 'prefix' => 'admin' ], function() {
...
/**
* Dashboard
*/
Route::get('/', 'Backend\DashboardController#index')->name('home.dashboard');
Route::get('/dasboard', 'Backend\DashboardController#index')->name('admin.dashboard');
});
Thank you #Sehdev...
Here is the final code that I am using. Although, even with both routes mentioned in the web.php, you can only see the route in the browser, that is written at end, which in my case is /dashboard. However, both (/, /dashboard) route are working now.
Route::namespace('Backend')->prefix('admin')->group(function() {
...
/**
* Dashboard
*/
Route::get('/', 'DashboardController#index')->name('admin.dashboard');
Route::get('/dashboard', 'DashboardController#index')->name('admin.dashboard');
});
Many thanks again :)
Related
I was working in laravel 8.x, I have developed API to register, but when i test using postman also in browser the url [1]: http://127.0.0.1:8000/api/register always returns 404 not found message.
below is my api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::group(['prefix' => 'v1'], function () {
Route::post('/login', 'UsersController#login');
Route::post('/register', 'UsersController#register');
Route::get('/logout', 'UsersController#logout')->middleware('auth:api');
});
can you please help?
Since you already put a route group "v1", all your routes must have that prefix, so just api/register wont work because that route doesn't exist inside your api.php, so infront of your routes just use
http://127.0.0.1:8000/api/v1/register
http://127.0.0.1:8000/api/v1/login
http://127.0.0.1:8000/api/v1/logout
Your routes looking fine.
I think you are forgetting to add http://127.0.0.1:8000/api/**v1**/register so please try with it once.
I'm new to Laravel and I inherited a project. I saw that there was a app/Http/Controller/Auth/RegisterController.php, but going to the websites /register gave me a 404 error. So I added this line to routes/web.php
Route::get('/register', 'RegistrationController#create')->name('register.create');
Route::post('/register', 'RegistrationController#store');
And now I can go to the url /register and sign up a new user without any issues.
I went into resources/views/auth/login.blade.php and added the line
Don't have an account? Sign up
But this gave me an error Route [register.create] not defined. View(.. path to login.blade.php
What did I do wrong?
Answer
The reason why you it's returning a 404 is because when you manually register the registration routes and you do it before the Auth::routes which registers one with the same key that overwrites yours. Hence why it's working if you move them after the Auth::routes.
What you could do is disable the register routes from the Auth facade:
Route::get('/register', 'RegistrationController#create')->name('register.create');
Route::post('/register', 'RegistrationController#store');
Auth::routes(['register' => false]);
If you plan on using Laravel's default registration system, you simply have to remove your manually registered routes and create the respective views and you can access the route with route('register');.
You can also check the other available routes generated by the Auth facade with php artisan route:list.
Note
Also, you do not need to group them in the web middleware when adding routes in routes/web.php because they are automatically in the middleware by the RouteServiceProvider.
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
I solved the problem. I made a guess changed this:
Route::group(['middleware' => ['web']], function () {
... other code ...
Route::get('/register', 'RegistrationController#create')->name('register.create');
Route::post('/register', 'RegistrationController#store');
Auth::routes();
... other code ...
});
To this
Route::group(['middleware' => ['web']], function () {
... other code ...
Auth::routes();
... other code ...
});
Route::get('/register', 'RegistrationController#create')->name('register.create');
Route::post('/register', 'RegistrationController#store');
And it worked. NOt sure if this is the right way to do it though
What is the correct way to authenticate all routes except login and register when I apply auth middleware in all controllers? Is there a way to apply auth middleware in one place and exclude login, register routes?
You can group all your authenticated routes like following, laravel provides a default middleware for auth and guest users
Route::group(['middleware' => ['auth']], function () {
Route::get('home', 'HomeController#index');
Route::post('save-user', 'UserController#saveUser');
Route::put('edit-user', 'UserController#editUser');
});
The above route names are just made up, please follow a proper naming convention for your routes and controllers. Also read about middlewares over here and about routing over here
you can apply middlewares in the routes.php file, what you need to do is to put all your routes on a group, and add the middleware 'auth' ( except the Auth::routes() which are already configured), for example :
Route::middleware(['first', 'second'])->group(function () {
Route::get('/', function () {
// Uses first & second Middleware
});
Route::get('user/profile', function () {
// Uses first & second Middleware
});
});
more information can be found in the docs: https://laravel.com/docs/5.7/routing#route-group-middleware
You can add middleware to your whole web.php route file by adding the middleware to your routes mapping in RouteServiceProvider.
Go to app/Providers/RouteServiceProvider.php and in mapWebRoutes(), change middleware('web') to middleware(['web', 'auth']):
protected function mapWebRoutes()
{
Route::middleware(['web', 'auth'])
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
This is (not?) totally unrelated but here's an example of a clean way to handle a lot of route files instead of throwing all your routes into a single web.php file:
Create a new method mapAdminRoutes():
protected function mapAdminRoutes()
{
Route::middleware(['web', 'auth:admin'])
->namespace('App\Http\Controllers\Admin')
->name('admin.')
->group(base_path('routes/admin.php'));
}
Map it:
public function map()
{
$this->mapWebRoutes();
$this->mapAdminRoutes(); // <-- add this
...
}
Create an admin.php file in your routes folder, then create your routes for Admin:
<?php
use Illuminate\Support\Facades\Route;
// This route's name will be 'admin.dashboard'
Route::get('dashboard', 'DashboardController#dashboard')->name('dashboard');
// This route's name will be 'admin.example'
Route::get('example', 'ExampleController#example')->name('example');
...
Now you can configure everything in 1 place, like prefix, name, middleware and namespace.
Check php artisan route:list to see the results :)
I'll try to explain this weird behavior as detailed as possible. The problem is, that the user is logged out after login. So, the login actually happens, session file is set in storage folder, but after another click it's logging me out. It is interesting that this is happening only on production/shared hosting, on localhost it is working fine. What I've tried so far:
Changed the file to database in session.php
Changed the session name - it works, but only for couple of hours!
Debugged Exceptions/handler.php unauthenticated method
Fiddled with Kernel.php and tried every possible way that came up in Google
Tried redefining and regrouping middleware groups
Here is my route file:
Route::group(['middleware' => ['web']], function () {
Route::prefix('admin')->group(function () {
/**
* Auth
*/
Route::get('/login', 'Admin\Auth\LoginController#showLoginForm');
Route::post('/login', 'Admin\Auth\LoginController#login');
Route::get('/logout', 'Admin\Auth\LoginController#logout');
Route::get('/', 'Admin\AdminController#index');
/**
* Structure
*/
Route::prefix('structure')->group(function () {
Route::resource('menu', 'Admin\Structure\MenuController');
Route::resource('template-group', 'Admin\Structure\TemplateGroupController');
Route::resource('property', 'Admin\Structure\PropertyController');
Route::resource('property-type', 'Admin\Structure\PropertyTypeController');
Route::resource('property-value', 'Admin\Structure\PropertyValueController');
Route::resource('property-value-order', 'Admin\Structure\PropertyValueOrderController');
Route::resource('template', 'Admin\Structure\TemplateController');
});
Route::resource('structure', 'Admin\StructureController');
/**
* Content
*/
Route::prefix('content')->group(function () {
Route::resource('page', 'Admin\Content\PageController');
});
Route::resource('content', 'Admin\ContentController');
});
});
i am new to laravel,
i have code in my controller's __construct like
if(Auth::check())
{
return View::make('view_page');
}
return Redirect::route('login')->withInput()->with('errmessage', 'Please Login to access restricted area.');
its working fine, but what i wants is. its really annoying to put these coding in each and every controller, so i wish to put this Verify Auth and redirect to login page in one place, may be in router.php or filters.php.
I have read some posts in forum as well as in stackoverflow, and added code in filters.php like below but that's too not working.
Route::filter('auth', function() {
if (Auth::guest())
return Redirect::guest('login');
});
Please help me to resolve this issue.
Laravel 5.4
Use the built in auth middleware.
Route::group(['middleware' => ['auth']], function() {
// your routes
});
For a single route:
Route::get('profile', function () {
// Only authenticated users may enter...
})->middleware('auth');
Laravel docs
Laravel 4 (original answer)
That's already built in to laravel. See the auth filter in filters.php. Just add the before filter to your routes. Preferably use a group and wrap that around your protected routes:
Route::group(array('before' => 'auth'), function(){
// your routes
Route::get('/', 'HomeController#index');
});
Or for a single route:
Route::get('/', array('before' => 'auth', 'uses' => 'HomeController#index'));
To change the redirect URL or send messages along, simply edit the filter in filters.php to your liking.
To avoid code repetition, You can use it in middleware. If you are using the Laravel build in Auth, You can directly use the auth middleware as given,
Route::group(['middleware' => ['auth']], function() {
// define your route, route groups here
});
or, for a single route,
Route::get('profile', function () {
})->middleware('auth');
If you are building your own, custom Authentication system. You should use the middleware which will check the user is authenticated or not. To create custom middleware, run php artisan make:middleware Middelware_Name_Here and register the newly created middleware.
It's absolutely correct what other people have replied.
This solution is for Laravel 5.4
But just in case, if you have more than one middleware applying to routes, make sure 'auth' middleware comes in the end and not at the start.
Like this:
Route::prefix('/admin')->group(function () {
Route::group(['middleware' => 'CheckUser', 'middleware' => 'auth'], function(){
});
});
Route::middleware(['auth'])->group(function () {
Route::get('dashboard','BackendController#dashboard')->name('dashboard');
});
This entry in the web.php route will take the user [who is not logged in] to the login page if (s)he tries to access a 'protected' URL, "dashboard" in this case.