I have a laravel route like below :
Route::group(['namespace' => 'Aggregate\Customer\Controller\v1_0','middleware' => 'jwt.auth', 'prefix' => 'api/v1.0/{lang}'], function () {
Route::put('customer/{id}', 'CustomerController#update_customer');
});
And i want to lang key on route 'prefix' => 'api/v1.0/{lang}' be first variable globally in all methods and in all controllers without manual added in all methods like :
See $lang
public function add_address_book($lang,$user_id,AddressBookRequest $request)
{
How can i do that?
One option is update the config var app.locale.
Route::group([
'namespace' => 'Aggregate\Customer\Controller\v1_0',
'middleware' => 'jwt.auth',
'prefix' => 'api/v1.0/{lang}'
], function () {
App::setLocale(app('request')->segment(3));
Route::put('customer/{id}', 'CustomerController#update_customer');
});
Then use
echo App::getLocale();
You can set the default locale and the fallback locale in app/config.php
Another option is to set up a singleton in the app container
Route::group([
'namespace' => 'Aggregate\Customer\Controller\v1_0',
'middleware' => 'jwt.auth',
'prefix' => 'api/v1.0/{lang}'
], function () {
app()->singleton('lang', function () {
return app('request')->segment(3);
});
Route::put('customer/{id}', 'CustomerController#update_customer');
});
Then in your controllers (or anywhere) you can use
echo app('lang');
Related
When I change the file-manager.php to 'routePrefix' => 'file' or 'routePrefix' => 'admin.file' the fm-button page still sends the following request.
Request URL: http://127.0.0.1:8000/file-manager/initialize
I changed the routes.php file as follows and only the routes was modified but the requests within the routes were not modified.
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () {
Route::group([ 'prefix' => 'file-manager', 'namespace' => 'Alexusmai\LaravelFileManager\Controllers', ], function () {...
In my Laravel 8 app I want to make route of ads/ad_locations editor
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
...
Route::group(['prefix' => 'ads'], function ($router) {
...
Route::resource(
'/{ad_id}/ad_locations',
AdLocationController::class
)->name('admin.ads.ad_locations');
But I got error clearing routes :
$ php artisan route:cache
ArgumentCountError
Too few arguments to function Illuminate\Routing\PendingResourceRegistration::name(), 1 passed in /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/routes/web.php on line 112 and exactly 2 expected
at vendor/laravel/framework/src/Illuminate/Routing/PendingResourceRegistration.php:110
106▕ * #param string $method
107▕ * #param string $name
108▕ * #return \Illuminate\Routing\PendingResourceRegistration
109▕ */
➜ 110▕ public function name($method, $name)
111▕ {
112▕ $this->options['names'][$method] = $name;
113▕
114▕ return $this;
1 routes/web.php:112
Illuminate\Routing\PendingResourceRegistration::name()
+3 vendor frames
5 routes/web.php:119
Illuminate\Support\Facades\Facade::__callStatic()
Which syntax is valid and how to ref in in blade file ?
MODIFIED BLOCK:
I modified in routes/web.php :
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
'edit' => 'admin.ads.ad_locations.edit',
]
]);
and run command with success :
php artisan route:cache
But in blade file using it :
<a class="btn btn-primary" href="{{ route('admin.ads.ad_locations.edit',[$ad_id, $nextAdLocation['id']]) }}">
{!! showAppIcon('edit') !!} Edit
</a>
I got error :
(Symfony\\Component\\Routing\\Exception\\RouteNotFoundException(code: 0): Route [admin.ads.ad_locations.edit] not defined. at /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:429)
[stacktrace]
Which way is correct ?
Thanks!
Function name() doesn't work with route::resource, because it's a collection of routes, it should be used with individual routes like:
Route::get('user/create', [UserController::class, 'create'])->name('user.create');
You can either supply a "names" array as the third parameter (options) parameter to the resource route, like:
Route::resource('user', UserController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
// etc...
]
]);
or, you can use with as keyword, like the one you have used in your route:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.ads.ad_locations.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class);
});
});
In the above snippet, I have used your complete route "admin.ads.ad_locations" name in "as" attribute's value. But, this last one will be treated as prefix for every single route like this:
admin.ads.ad_locations.{ad_id}.store
admin.ads.ad_locations.{ad_id}.create
...
You can also, use function names() like below:
Route::resource('/{ad_id}/ad_locations', PhotoController::class)->names([
'create' => 'admin.ads.ad_locations.build'
'show' => 'admin.ads.ad_locations.view'
]);
Answer For Rectified Section of the Question:
Because you are using "as" attribute in your route as well, the route names generated for you will be:
admin.admin.ads.ad_locations.store
admin.admin.ads.ad_locations.create
admin.admin.ads.ad_locations.create
You can either remove the as attribute from your route, like:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
'edit' => 'admin.ads.ad_locations.edit',
]
]);
});
});
or, you can avoid using "admin." in your routes names, like:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'ads.ad_locations.index',
'store' => 'ads.ad_locations.store',
'edit' => 'ads.ad_locations.edit',
]
]);
});
});
In my Laravel 5.8 app when there are no data in session I need to redirect to some default control.
I do
return redirect()->route('admin.oauthAdminCallback/' . $form_action);
When in routes/web.php defined :
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback/{form_action}', [ 'uses' => 'Admin\EventsController#oauthAdminCallback']);//->name('oauthAdminCallback');
But I got error :
Route [admin.oauthAdminCallback/calendarActionUpdate] not defined.
If in first line $form_action has value : “calendarActionUpdate”.
Which is correct way ?
MODIFIED :
I tried this way
return redirect()->route('admin.oauthAdminCallback',$form_action);
and this way
return redirect()->route('admin.oauthAdminCallback')->with([
'form_action' => $form_action,
]);
But in both cases I do not have amy error but method was not called!
In my routes/web.php :
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback', [ 'as' => 'oauthAdminCallback', 'uses' =>'Admin\EventsController#oauthAdminCallback']);
// The method below is not called!
public function oauthAdminCallback()
{
session_start();
die("-1 XXZ oauthAdminCallback");
return redirect( is ignored and I can not understand why?
So you're calling the route by its name so
try this
return redirect()->route('admin.oauthAdminCallback',$form_action);
Mention your route as
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback', [ 'as' => 'oauthAdminCallback', 'uses' => 'Admin\EventsController#oauthAdminCallback']);
});
And your callback as below
return redirect()->route('admin.oauthAdminCallback', $form_action);
Tried and tested.
I am building a modular application in laravel. I created a User module and here is the routes:
<?php
Route::group(['middleware' => 'web', 'namespace' => 'Modules\User\Http\Controllers'], function()
{
Route::get('/', 'UserController#index');
Route::get('login', 'LoginController#showLoginForm')->name('login');
Route::post('login', 'LoginController#login');
Route::post('logout', 'LoginController#logout')->name('logout');
});
Route::group(['middleware' => 'admin', 'prefix' => 'user', 'namespace' => 'Modules\User\Http\Controllers'], function()
{
Route::get('register', 'RegisterController#showRegistrationForm')->name('register');
Route::post('register', 'RegisterController#register');
});
The route('login') statement returns the url to login page and it works well. Inside the config.php I need to access this function as follows
<?php
return [
'name' => 'User',
'menu' => [
'weight' => 1,
'item' => [
'Login' => [route('login'), 'guest'],
'Register' => [route('register'), 'guest'],
]
]
];
Inside this file the error Route [login] not defined. is reported. Why is this undefined in there?
I also tried adding the following line
namespace Modules\User\Http\Controllers;
But it still not working
thanks
I have a controller That have multiple functions with the same router so I am getting error exception.
Please Guide me for This error
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::get('/dashboard','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
You can't, the solution is :
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::get('/chart','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
Or you can call multiple functions on the same url, one with "get" method, and an other with "post" for example :
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'admin']], function()
{
Route::post('/dashboard','DashboardController#chart');
Route::get('/dashboard','DashboardController#index');
});
But the Route::post() is accessible only after a form submission with method post.