Laravel grouping routes with one name - php

i have this below route and that can work correctly
Route::get('admin/login', array('as'=>'login', function()
{
return View::make('back_end.login');
}));
app
views
back_end
layouts
index.blade.php
main.blade.php
profile.blade.php
login.blade.php
for admin i have any view for show and i want to grouping that with admin perfix. after this action and use
http://localhost/laravel/public/admin/login
http://localhost/laravel/public/admin/profile
URL i get this error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
this is my routes:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('login', function()
{
return View::make('back_end.login');
});
Route::get('index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('profile', function()
{
return View::make('back_end.layouts.profile');
});
});
how to fix this routes. please help me

I had the same issue recently. Here is a slimmed down version of the routing that I used, including a catch all. I was routing to controllers, however you can replace that syntax with a function, the rout will be handled the same.
Route::group(array('prefix' => 'admin'), function(){
Route::get('/','AdminController#index');
Route::resource('users', 'UserController');
Route::get('settings','AdminController#settings');
/* Catch all route */
Route::any('{all}', function($uri){
return Redirect::to('admin')
->with('flash_error', "The administration page 'admin/$uri' could not be found.");
})->where('all', '.*');
});
As always, make sure to run composer dump-autoload after updating the routes. This worked successfully for me. You will only need the '/' on the relative 'base' route.

Make changes (add a preceding slash / to each routes inside the admin group) as given below:
Route::group(array('prefix' => 'admin'), function()
{
Route::get('/login', function()
{
return View::make('back_end.login');
});
Route::get('/index', array('as'=>'dashboard'), function()
{
return View::make('back_end.layouts.index');
});
Route::get('/profile', function()
{
return View::make('back_end.layouts.profile');
});
});
It should be /login instead of login and same for each one.

Related

Laravel 8 prefix to current route

Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::get('/', [App\Http\Controllers\Admin\IndexController::class, 'index']);
});
and when I go to this url http://127.0.0.1:8000/admin/
it shows error: The requested resource /admin/ was not found on this server.
Try using below approach:
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function(){
Route::get('/', [App\Http\Controllers\Admin\IndexController::class, 'index']);
});
Try this code:
Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::get('', 'IndexController#index');
});
Please look inside your Public folder. You probably created a folder called 'admin'. Rename that into something else or remove it if you're not using it.

Laravel 5 Auth Filter do not work

I just tried Laravel 5 after a time at 4.2..
The docs says it is possible to use 'before' => 'auth' as always, but for me it does not work.
I have no idea whats wrong, I have read the docs, search on internet but seems not to find anything.
My code looks as:
$router->group(['before' => 'auth'], function($router)
{
//
$router->get('admin', function()
{
return View::make('admin.index');
});
//
$router->get('login', function()
{
return View::make('admin.login');
});
});
Anyone can see what I doing wrong here?
In laravel5 filters are removed. Instead you can use middleware classes which are more clean.
In this blog you can read more about the middleware classes and that they're a replacement of filters.
If you want to do it with self written routes you can use this:
Route::group(['middleware' => 'auth'], function()
{
Route::get('admin', function()
{
return View::make('admin.index');
});
Route::->get('login', function()
{
return View::make('admin.login');
});
});

Prioritizing Laravel 4 routes

I have routes like this:
Route::group(array('before' => 'installed'), function() {
Route::group(array('before' => 'auth_admin', 'prefix' => 'admin'), function()
{
Route::group(array('prefix' => 'gag'), function() {
Route::get('/', 'Admin\\GagController#index');
Route::get('delete/{id}','Admin\\GagController#delete');
});
});
});
I need to prevent users from deleting content on my demo application. So I added the following piece of code before my actual routes.
if(App::environment() === 'demo')
{
Route::get('admin/gag/delete/{id}', function() {
die("You can't delete anything on demo application.");
});
}
//Actual routes are at the below.
However, it doesn't work when Route::get('delete/{id}','Admin\\GagController#delete'); is there. Somehow, Laravel ignores my if block and priorities this route. (Although if block is at the top.)
Looks like routes.php parses my if block after routes are parsed.
How can I make it so Laravel prioritizes my demo routes? I just want to restrict access to some features like this.
Ps. I don't want to add all the routes in if blocks. I just want the routes in my if block to be prioritized.
Route filters are better to do this sort of restrictions:
Route::filter('checkDemo', function()
{
if (App::environment() === 'demo')
{
return Redirect::to('home')->withMessage('You can''t delete anything on demo application.');
}
});
And set the filter to your route:
Route::group(array('prefix' => 'gag', 'before' => 'checkDemo'), function()
{
...
});
Or you can filter just that particular route:
Route::get('delete/{id}', array('before' => 'checkDemo', 'uses' => 'Admin\\GagController#delete'));

Pattern filters in Laravel 4

I want to make a pattern route, to redirect users to login page when they are not logged in.
I searched but couldn't find a solution. as always Laravel's Documentation is useless!
I have this in my filter.php
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('login');
});
Route::filter('auth.basic', function()
{
return Auth::basic();
});
And this route in my routes.php
Route::when('/*', 'auth' );
but It's not working. How can I do that?
Change the route declaration for login like this
Route::get( '/login', array('as' => 'login', 'uses' => 'UserController#getLogin') );
In your filters.php use
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
So, you can use
Route::when('*', 'auth' );
You can also use
Route::group(array('before' => 'auth'), function ()
{
// Define all routes here with auth
Route::get('my_first_route', function(){ /... });
Route::post('my_second_route', 'MyController#myAction');
});

How to use Laravel's wildcards on route group before auth?

I have this code below in my router.php
Route::group(array('before' => 'auth'), function()
{
Route::get('account/(:all?)', function() {});
Route::get('facebook/(:all?)', function() {});
});
Route::controller(Controller::detect());
It works well when the user is not logged in. But once he is successfully logged in and gets redirected to the requested page, the page is not displaying anything; just a blank page. I have tried to use :any instead of :all and it does the same thing.
Can anybody identify the problems?
Your routes are mapped to empty closures. You need to return something or map them to controllers.
Route::get('account/(:any?)', function() {
return "Hello World";
});
Route::get('account/(:any?)', function() {
return View::make('accounts.index');
});
//assuming you have an AccountController.php
Route::get('account/(:any?)', 'account#index');
//automatically route all methods of a controller
Route::controller('account');
Check out the laravel docs on routing.
Apparently, I did not find the better solution for using the group filter. The way I do it now to redirect guests to auth is this:
Route::filter('before', function()
{
$open_routes = array(
'',
'home',
'auth',
'help'
);
if(!in_array(URI::segment(1), $open_routes) && Auth::guest()) {
return Redirect::to('/auth/login');
}
});

Categories