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
Related
I have two kind of routes, admin routes and frontend routes.
The frontend routes
Route::get('{locale?}/', ['uses' => '\App\Http\Controllers\loadViewController#home']);
Route::get('{locale?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#index']);
Route::get('{locale?}/{template?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#detail']);
The backend routes
Route::prefix('admin/dashboard')->group(function () {
Route::get('/', 'DashboardController#index')->name('dashboard');
});
Now when i type admin/dashboard or api/admin, laravel uses the frontend routes to load the views, while i want the backend views to be loaded.
So to filter out the backend routes i tried this
Route::group(['where' => ['page' => '^(?!admin|api)$', 'template' => '^(?!admin|api)$']], function ({
Route::get('{locale?}/', ['uses' => '\App\Http\Controllers\loadViewController#home']);
Route::get('{locale?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#index']);
Route::get('{locale?}/{template?}/{page}', ['uses' => '\App\Http\Controllers\loadViewController#detail']);
});
which obviously did not work
Also the frontend routes should not have something like /website, they should all start with /
My question is: How can i load the backend and frontend routes separately without interfering when called, even if they have the same url length in terms of parameters, keep in mind that the admin routes always start with /admin or /api.
Note: i can't put the backend routes before the frontend routes
Thanks in advance!
If you want to you could put a constraint on the locale route parameter:
Route::pattern('locale', '^(?!(api|admin)$)(\w*)');
You can put this in the boot method of you RouteServiceProvider and it will now not allow the locale route parameter to match for 'api' or 'admin'.
You can register seperate routes in RouteServiceProvider. Following is how you can do it.
Inside RouteServiceProvider.php do:
public function map()
{
$this->mapFrontendRoutes();
$this->mapAdminRoutes();
}
Definition of mapFrontendRoutes():
protected function mapFrontendRoutes()
{
Route::prefix('{locales?}')
->middleware('frontend')
->namespace($this->namespace.'\Frontend')
->group(base_path('routes/frontend.php'));
}
Definition of mapAdminRoutes():
protected function mapAdminRoutes()
{
Route::prefix('admin')
->middleware('admin')
->namespace($this->namespace.'\Admin')
->group(base_path('routes/admin.php'));
}
I personally find this very useful, helps to declare interference free and logical routes. Open to feedback.
The simple way is to group both url's as separate groups. Example as follows :
Route::group(['prefix' => 'admin', 'as' => 'admin'], function () {
Route::post('/dashboard', 'AdminController#dashboard');
});
Route::group(['prefix' => 'home', 'as' => 'home'], function () {
Route::get('/record/{id}', 'HomeController#getRecord');
});
I have set a locale.
The url should usually start with http://example.com/en/...../.....
But for some(not all!) routes I would like to add something in front of the locale.
for example http://example.com/api/settings/en/...../.....
Is that even possible with the standard LaravelLocalization or what could be a solution to this?
thx in advance
edit:
my routing files all start with something like
// Index page
Route::get('/', [
'as' => 'index',
function () {
return view('index');
}, ]);
// Campaign mode
Route::group([
'prefix' => 'campaign',
'as'
So there is no language set in the routes itself
Place your API routes above the language group of routes
$router->group(['prefix'=>'api'], function($router)
{
$router->get('/', 'APIController#index');
});
// This would product api/
$router->group(['prefix'=>'{lang?}'], function($router)
{
$router->get('home', 'HomeController#index');
});
// This would produce /en/home
Your routes of course will be different, but that's the gist
I have the following route in my laravel route file:
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
The problem with the above route is , its overriding the below route:
Route::resource('admin', 'adminController');
I want to keep my resource route, but how do i keep my resource ? is there a way around this ??
Modify your route file like this.
Route::resource('admin', 'adminController');
Route::get('/{id}' , [ 'uses' => 'PagesController#show', 'as' => 'getArticle' ]);
Route files executed in the order they are defined.
If you define Route::get('/{id}',.... in the beginning and set your url like http://your-site/admin, the admin section will be considers as the id for the Route::get('/{id}',.... route. So you need to keep it in your mind when you define your route.
just move this route in the end of the web.php file.
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
There are two options:
move Route::get('/{id}', ...) after Route::resource(...)
or add a pattern to Route::get() if id is numeric Route::get('/{id}', ...)->where('id', '[0-9]+');
I need to localize my website. I am using this package: https://github.com/mcamara/laravel-localization
My route group is:
Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController#garage']);
//OTHER ROUTES
});
And i want to localize link to this route. If i am using
href="{{ route('garage') }}
everything is ok, link looks like "www.example.com/locale/garage".
But if i am using
href="{{ url('/garage') }}
my localization doesn't work, link looks like "www.example.com/garage".
Is there some way to localize links made with URL helper?
LaravelLocalization package includes a middleware object to redirect all non-localized routes to the corresponding "localized".
So, if a user navigates to http://www.example.com/test and the system has this middleware active and 'en' as the current locale for this user, it would redirect him automatically to http://www.example.com/en/test.
To active this functionality go to app/Http/Kernel.php and add these classes in protected $routeMiddleware = [] at the beginning and then in your routes file bring these changes:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController#garage']);
//OTHER ROUTES
});
And your problem will be solved.
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.