Get route list from parent route group in laravel - laravel

Here is the simple routing code: (i take it from official site just for example)
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
});
Route::get('user/profile', function () {
});
});
Is it possible to get all routes from this group programmatically? Thank you.

Hope, this function will help you..
In Routes.php file --
Route::get('routes', function() {
\Artisan::call('route:list');
echo '<pre>'; print_r(\Artisan::output());
});

Related

How to exclude routes from route GET

New to laravel so Im trying to do something that doesn't work. I'm building a backend dashboard with login & register page, these 2 pages should have different layouts. How can I do this as the way below is not working. I'm using VUE.
Of course is {any} gonna get all so is there a way to exclude pages from this?
web.php
Route::get('/{any}', function () {
return view('layouts.vue');
})->where('any', '.*');
Route::get('/login', function () {
return view('entry.vue');
})->name('login');
You should add your route on top of route with {any}.
For example:
Route::get('/somewhere', function () {
return view('somewhere');
});
Route::get('/login', function () {
return view('entry.vue');
})->name('login');
Route::get('/{any}', function () {
return view('layouts.vue');
})->where('any', '.*');

In Laravel, how do you use a "cannot" in a Route::middleware group?

I have my own Laravel gates defined "isAdmin" and "isManager". The "auth" middleware simply means the user is logged in. My routes look like this (greatly simplified):
Route::middleware(['auth', 'can:isAdmin'])->group(
function () {
Route::get('/', function() { return response("Admin home"); });
}
);
Route::middleware(['auth', 'can:isManager'])->group(
function () {
Route::get('/', function() { return response("Manager home"); });
}
);
But what do I do for a route for a logged-in user who is neither an admin nor a manager? I would like to do this:
Route::middleware(['auth', 'cannot:isManager', 'cannot:isAdmin'])->group(
function () {
Route::get('/', function() { return response("Who are you, anyway?"); });
}
);
Anybody have any ideas? Thanks.
you can set new rule (gate) as notAdminAndManager in app/Providers/AuthServiceProvider.php on boot method
here is some example
Gate::define('isNotAdminAndManager', function (User $user) {
return (! in_array($);
});

Laravel routing doesn't work

This is my route.php file:
Route::get('/users', function () {
return 'Users!';
});
But when I enter the url:
http://localhost:63342/quickstart/public/users
I get a 404 not found exception.
But if I change
Route::get('/users', function ()
to
Route::get('/', function ()
The following url functions well:
http://localhost:63342/quickstart/public/index.php
Try
Route::get('users', function () {
return 'Users!';
});
You should remove the:
/quickstart/public/
and just use:
http://localhost:63342/users

Laravel 5.* root path in subdomain

I have base domain routes and subdomain routes. For example if I request subdomain.example.com/test it will return me right answer. But if I want to request subdomain.example.com it will execute code from root domain.
Route::get('/', function() {
// Main
});
Route::get('/path', function() {
// ..
});
Route::group(['domain' => 'subdomain.example.com'], function()
{
Route::get('/', function() {
// How to request this part?
});
Route::get('/test', function() {
// Works
});
}
Changing the order will help - Laravel keeps the routes in order, and checks them one-by-one, so by moving the subdomain's routes above the main routes they'll get found first and used, with the global routes as fallbacks for other domains.
Route::group(['domain' => 'subdomain.example.com'], function()
{
Route::get('/', function() {
// How to request this part?
});
Route::get('/test', function() {
// Works
});
}
Route::get('/', function() {
// Main
});
Route::get('/path', function() {
// ..
});

How can I redirect a route using laravel?

I use group to help visualize my project's routes.php, now the problem is
for example, when a user access to "/dir", I want to make it be redirected to "/dir/ele"
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::controller('/', 'dir\eleController');
});
Redirect::route('ele');
}
Why is this not working?
The route dir/ele is going to a controller, but you are doing the redirect in your routes.php instead of in the controller.
You should use a closure route and do everything in the routes.php or use a controller and move the redirect to the controller:
Closure route in routes.php:
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::get('/', function() {
return Redirect::to('ele');
});
});
});
Which can be simplified to:
Route::group(['prefix' => 'dir'], function () {
Route::get('ele', function(){
return Redirect::to('ele');
});
});
Or use the controller way:
Routes.php
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::controller('/', 'eleController#redirect');
});
}
eleController.php
class eleController extends BaseController {
function redirect() {
return Redirect::to('ele');
}
}

Resources