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
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 use Laravel 7 for an API project, I have created a JWT Middleware, and I want to apply it to all my routes, except 2 of them.
For now I have in my routes/api.php :
Route::prefix('v1')->group(function () {
Route::get('ping', 'Api\Ping\PingController#ping');
// auth routes
Route::group(['prefix' => 'login/'], function () {
Route::post('login', 'Api\Auth\AuthController#login');
Route::group(['middleware' => 'jwt:api'], function() {
Route::get('me', 'Api\Auth\AuthController#me');
Route::post('refreshToken', 'Api\Auth\AuthController#refresh');
Route::post('logout', 'Api\Auth\AuthController#logout');
});
});
Route::group(['middleware' => 'jwt:api'], function() {
Route::resource('users', 'Api\User\UserController');
// my other routes protected .....
I don't like this approach because I need to copy the middleware.
I tried this approach :
Route::group(
[
'middleware' => ['jwt:api', ['except' => 'login/login']],
'prefix' => 'v1/',
], function() {
But I have this error :
Illegal offset type in isset or empty
Is it possible ? I want to group everything in my route file.
Possible solutions:
You can pass additional parameters to middleware via dots and check in middleware to do not use passed routes
Also, you can overwrite middleware and add some property\constant with array of excepts, like in csrf middleware
Implement ability in Laravel core to pass except array as in your exmaple and make a PR to framework github
Left it as you have done
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.');
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