Laravel routes not working with subfolder - laravel

Using Laravel 5 migrated from 4.2 now laravel 5 is installed in subfolder "abc" do i have to write abc/warehouse for every route ? previously it was /warehouse. i want to use all existing routes like /warehouse inside subdirectory abc.
i am on localhost xampp with port 81. http://localhost:81/warehouse
any one here with quick solution

You use prefix when defining routes:
Route::prefix('abc')->group(...)
Route Prefixes
Route::prefix('abc')->group(function () {
Route::get('warehouse', function () {
// Matches The "/abc/warehouse" URL
});
});
Ideally you should do it in the RouteServiceProvider
Route::middleware('web')
->prefix('abc')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
This way everything in the routes file is prefixed and you dont need the extra group wrapping.
Here's the example from the 5.0 docs:
Route::group(['prefix' => 'admin'], function() {
Route::get('users', function() {
// Matches The "/admin/users" URL
});
});

what you can do is add a line in RouteServiceProvider in mapWebRoutes function like this
public function mapWebRoutes()
{
//default
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
//subfolder
Route::middleware('web')
->prefix('abc')
->namespace($this->namespace)
->group(base_path('routes/abc.php'));
}
then create a file inside routes/abc.php and copy paste all your routes inside it
Route prefix https://laravel.com/docs/5.6/routing#route-group-prefixes
For laravel 5.0 you have to wrap inside Route::group
Route::group(['prefix' => 'abc', 'namespace' => 'Auth'], function(){
//define all your routes here
Route::post('login', 'AuthController#login');
});
Namespace: Here i have define Auth in namespace that means my all controllers like AuthController files should be inside app/Http/Controllers/Auth folders.
Laravel route 5.0 https://laravel.com/docs/5.0/routing#route-group-prefixes
For laravel 5.0 namespace structure check this https://laravel.com/docs/5.0/structure

Related

Laravel 6: disable all routes for guest except home and login

I need to disable all routes for guests in Laravel except '/' and 'login' pages.
Does that possible to implement it routes/web.php ?
Yes. In your routes/web.php file, make sure to define your protected routes under the auth middleware group.
routes/web.php
Route::get('/', function() {
// / route
});
Route::get('/login', function() {
// login page
});
Route::middleware(['auth'])->group(function () {
// define your routes here
// they'll be protected
});
Official documentation
Since Laravel 7.7 you can use excluded_middleware property eg:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/', 'HomeController#index');
...
});

Laravel Route not defined for registercontroller

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

Apply Auth Middleware to All Laravel Routes

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 :)

Laravel except a single route from auth middleware

I have a route group which is protected by the auth middleware, and inside of this group I want to except one route. But this route is also located in another route group. So when I try to move it out of this group, it is not working.
How can I fix this problem, and except a UrlProfile function from auth middleware?.. I am using Laravel 5.1
Route::group(['middleware' => 'auth'], function () {
// some other routes ...
Route::group(['namespace' => 'Lawyer'], function() {
Route::get('profile/{name}', 'ProfileController#UrlProfile');
}
};
Can you try this?
Route::group(['namespace' => 'Lawyer'], function () {
Route::get('profile/{name}', 'ProfileController#UrlProfile');
Route::group(['middleware' => 'auth'], function() {
..
..
..
)};
)};
If I understood your problem correctly, This should also work.
You can add this in your controller.
You can insert the name of your function in the except section and it will be excluded from the middleware. [Reference]
public function __construct()
{
$this->middleware('auth')->except(['yourFunctionName']);
}

How admin route in laravel 4

My folder structure is following:
Controller>admin>`loginController`
view>admin
model>admin
In LoginController includes authorization process
I have used routes:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'LoginController');
});
But i also found error 'Controller method not found.
You either have to use Route::controller() or specify the action that you want to route to. For example:
Controller
public function showLogin(){
return View::make('login');
}
Route
Route::get('/', 'LoginController#showLogin');
(With the # part you tell Laravel what controller method you want to call)

Resources