For example , I have a routes that is for the admin page to manage books, a route is set like this:
Route::resource('books','Admin\BookController');
It generated few routes for insert / update/ delete etc... automatically
/books/create
/books/1/edit
The problem is , it is admin page and I would like the link to be
/admin/books/create
/admin/books/1/edit
How to specific the resource to be admin one ? it auto have prefix of /admin/ Thanks
Updated:
If you need prefix for a multiple routes, you should use route group:
Route::group(['prefix' => 'admin'], function()
{
Route::resource('books','Admin\BookController');
});
Or, if you need to use just one controller, you could just do this:
Route::resource('/admin/books','Admin\BookController');
Change your route to
Route::resource('admin/books','Admin\BookController');
Just to add to Alexey's Answer. I'm using Namespace too with group. Below is the example.
Route::group([
'prefix' => 'admin',
'namespace' => 'Admin',
'middleware' => 'admin.routeNeedsPermission:view-admin-management',
], function() {
Route::resource('books','BookController');
});
By that way you don't need to write admin in all your routes.
Related
I have group of middleware in which I want to add the route but it does not working, the route group is following
Route::group(
[
'domain' => 'admin.'.env('APP_DOMAIN'),
'as' => 'admin.'
],
function () {
Route::group(['namespace' => 'Admin'], function() {
/* I am trying to add route here */
});
})
I need to add following route
Route::post('/dashboard/tokens-sale-record','Admin\DashboardController#totalSaleForChart')->name('tokensSaleRecords');
When I add this route inside above group then it is not working but when I add outside it is working fine. I am using route in ajax.
Can someone kindly let me know what is the issue. I would like to appreciate.
Thank you so much.
Because you already defined Admin\ namespace path in parent group. That's way, You don't use Admin again namespace path in routes in the group.
Can you try following route define.
Route::post('/dashboard/tokens-sale-record','DashboardController#totalSaleForChart')->name('tokensSaleRecords');
If you using again Admin\Dashboard, Laravel searching it DashboardController as the Admin\Admin\DashboardController.
Route::group(
[
'domain' => 'admin.'.env('APP_DOMAIN'),
'as' => 'admin.'
],
function () {
Route::group(['namespace' => 'Admin'], function() {
Route::post('/dashboard/tokens-sale-record','DashboardController#totalSaleForChart')->name('tokensSaleRecords');
});
});
There is no need to write admin before calling controller. It will check for Admin\Admin\DashboardController.
If you are not able to find right route then use php artisan route:list | grep tokens-sale-record to check for the right route.
I'm using this package in my project and there have default package routes.
Like this:
I want use this route in my controller. I'm trying to use with name but it did not work this way.
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Voyager::routes();
});
And
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
})->name('admin');
I'm trying to use like this:
I want to give access like this, as if I'm trying to access 'admin' route then I could access all routes under these route group. I don't know how I will do that?
Please help me.
You cannot redirect to route with name admin. because such route doesn't exist.
When you use:
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Voyager::routes();
});
it means all routes created by Voager::routes() will have name starting with admin. but it doesn't mean admin. route exist.
So I assume you should instead use rather admin.voyager.dashboard instead, so you should rather use:
return redirect()->route('admin.voyager.dashboard');
instead of:
return redirect()->route('admin.');
I have a Laravel 5.2 application where I want to display the same page on 2 different domains / routes. I have it working using the following route structure:
The routes to my primary domain:
Route::group(['domain' => 'www.primarydomain.com',
'prefix' => 'demo-page']), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The routes to my secundary domain (note: no prefix!):
Route::group(['domain' => 'www.secundarydomain.com',]), function(){
Route::get('/my-page', 'MyController#index');
Route::get('/my-second-page', 'MyController#getPageTwo');
}
The idea is that both routes will work, and they do. Both www.secundarydomain.com/my-page and www.primarydomain.com/demo-page/my-page work.
The issue is when I now want to generate a link to my second page. For building my URL's in my views, I'm using the following function to generate a link to my-second-page:
url('/my-page')
This function always generates a link to www.primarydomain.com/my-page, while I need a link to www.primarydomain.com/demo-page/my-page.
Is there any easy solution to resolve this? Can this be resolved using middleware, or will a custom URL function be needed?
Expected results:
url('my-page') on www.primarydomain.com should generate a link to www.primarydomain.com/demo-page/my-page
url('my-page') on www.secondarydomain.com should generate a link to www.secondarydomain.com/my-page
Easiest way to do that is to create your own helper, like custom_url() and use it instead of url().
You can look how original url() helper works and create similar one. Look here:
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/helpers.php#L806
You can assign aliases to your routes.
Route::group(['domain' => 'www.primarydomain.com', 'prefix' => 'demo-page']), function(){
Route::get('/my-page', [
'as' => 'my_page_prefixed',
'uses' => 'MyController#index'
]);
Route::get('/my-second-page', [
'as' => 'my_second_page_prefixed'
'uses' => 'MyController#getPageTwo'
]);
}
And then you can call your aliased route on your blade templates by using {{ route('my_page_prefixed') }} or any other alias.
I have routes for my project and I'm using Route::when('*', 'auth') to protect my routes by implement auth filter on every Route like given below:
// All the following routes must be filtered by the 'auth' filter.
Route::when('*', 'auth');
Route::resource('route_1', 'Controller_1);
Route::controller('route_2', 'Controller_2');
Route::get('route_3', 'Controller_3#method_1');
It's clear that the user can not access the routes as a guest or in other words without log into.
Now I'v to use a couple of routes which could be accessed without login. I'm using the following code but it's not working and also implement auth filter on route_0:
// Following two routes must not be filtered by the 'auth' filter.
Route::get('route_0', 'Controller_0#getMethod');
Route::post('route_0', 'Controller_0#postMethod');
// All the following routes must be filtered by the 'auth' filter.
Route::when('*', 'auth');
Route::resource('route_1', 'Controller_1);
Route::controller('route_2', 'Controller_2');
Route::get('route_3', 'Controller_3#method_1');
How can I remove auth filter from route_0? I also don't want to use auth filter separately on each route or controller. Any solution please?
You can use Route group like following
Route::get('/', array('as' => 'home','uses' => 'HomeController#index'));
Route::group(array('before' =>'auth'), function()
{
Route::get('about', array('as' => 'about','uses' => 'HomeController#about'));
}
);
Put the routes those required to be filtered in the group and other outside. You can use multiple group too.
You can read about it here https://laravel.com/docs/4.2/routing#route-filters
You can use a route group that uses a given route filter.
Route::group(['before' => ['auth']], function() {
Route::resource('route_1', 'Controller_1');
Route::controller('route_2', 'Controller_2');
Route::get('route_3', 'Controller_3#method_1');
});
// Other non filtered routes.
Documentation: https://laravel.com/docs/4.2/routing#route-filters
Note: In Laravel 5.*, we don't use filters anymore, instead, we use middlewares: https://laravel.com/docs/master/middleware.
You can use whenRegex instead of when and exclude the route path from matching when applying the filter:
Route::whenRegex('/^((?!route_0).)*$/', 'auth');
In my application I need to set a route that respond to any pages.
I have put in my routes.php file:
// Handle all the pages
Route::get('{slug?}', 'Frontend\PageController#show');
and it works, the problem is that now I need an admin section so in my routes.php I added before the previous route:
Route::group( [ 'prefix' => 'admin', 'middleware' => config('admin.filter.auth') ], function(){
// other routes
} );
The problem is that the url site.com/admin has been catch by the wildcard so I'm not able to visit that url.
This is the full routes file:
//admin routes
Route::group( [ 'prefix' => 'admin', 'middleware' => config('admin.filter.auth') ], function(){
Route::get('upload-file', 'Backend\UploadController#index');
Route::post('upload-file', 'Backend\UploadController#uploadFile');
Route::get('load-contacts', 'Backend\UploadController#loadContacts');
} );
// Handle all the pages
Route::get('{slug?}', 'Frontend\PageController#show');
How should I manage this?
You can easily achieve that by providing a regular expression that should be used to match your slug parameter:
Route::get('{slug?}', 'Frontend\PageController#show')->where('slug', '.*');
The reason the /admin path is caught by the catch-all route is that you don't have a route defined for /admin URL. The only URLs that will be handled separately are
GET /admin/upload-file
POST /admin/upload-file
GET /admin/load-contacts