im using Laravel 5, and i have this route:
Route::group(['prefix' => '{lang}/', 'middleware' => 'SetLanguage'], function($lang){
//element Show
Route::get('/'.trans('routes.element-container').'/{slugName}', 'ElementController#showByName');
});
My middleware is this:
public function handle($request, Closure $next)
{
if (in_array($request->lang, config('app.all_langs'))){
//exit("SETTING ON ".$request->lang);
App::setLocale($request->lang);
}else{
//exit("SETTING ON en");
App::setLocale('en');
}
return $next($request);
}
If i un-comment the two exit it works, but, the function "trans" on route side is not working, seems to trans only in default lang.
why the "trans" function is called before the middleware?
I have test with 'before' and 'after', but no work...
If I understood you correctly, you could consider something like this:
Route::group(['prefix' => '{lang}/', 'middleware' => 'SetLanguage'], function() {
foreach (config('app.all_langs') as $language) {
$translatedRoute = trans('routes.element-container', [], $language);
Route::get("/$translatedRoute/{slugName}", 'ElementController#showByName');
}
});
But this will also register routes for e.g. /de/element-container/element-a.
Alternative:
foreach (config('app.all_langs') as $language) {
Route::group(['prefix' => $language, 'middleware' => 'SetLanguage'], function() {
$translatedRoute = trans('routes.element-container', [], $language);
Route::get("/$translatedRoute/{slugName}", 'ElementController#showByName');
});
}
This will register /en/element-container/element-a and /de/Elemente-Behälter/element-a, but not /de/element-container/element-a
Related
I have a problem with redirect back in laravel 8.
When I access page url like: localhost:8000 and change languages from english to japan. Browser back to localhost:8000 and update new selected language. But when I access dynamic url like localhost:8000/post/tin-tuc/recruiment and select language. Browser back to url like this localhost:8000/post/tin-tuc/language/ja and show 404 page error. Tks for your help.
Here is my code:
Web.php
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('post/{category_slug}/{content_slug}', [App\Http\Controllers\PostController::class, 'detail'])->name('post');
Route::get('post-detail/{content_slug}/{post_slug}', [App\Http\Controllers\PostDetailController::class, 'detail'])->name('post.detail');
Route::get('language/{locale}', function ($locale) {
app()->setLocale($locale);
session()->put('locale', $locale);
return Redirect::intended('/');
});
Localization middleware
public function handle(Request $request, Closure $next)
{
if (Session::has('locale')) {
App::setLocale(Session::get('locale'));
}
return $next($request);
}
config/app.php
'locale' => 'en',
'fallback_locale' => 'en',
'available_locales' => [
'English' => 'en',
//'Vietnamese' => 'vi',
'Japanese' => 'ja',
],
app/http/Provider/AppServiceProvider
public function boot()
{
view()->composer('partials.language_switcher', function ($view) {
$view->with('current_locale', app()->getLocale());
$view->with('available_locales', config('app.available_locales'));
});
$categoriesGeneral = Category::where('status', config('constants.status.Alive'))->get();
view()->share('categoriesGeneral', $categoriesGeneral);
$contentItems = Content::join('categories', 'contents.category_id', '=', 'categories.id')
->where('contents.status', config('constants.status.Alive'))
->where('categories.status', config('constants.status.Alive'))
->select('contents.*', 'categories.category_name', 'categories.slug AS category_slug')
->get();
view()->share('contentItems', $contentItems);
}
in web.php :
Route::group(['middleware'=>['checklang','checkmoney']],function(){
Route::get('/', function () {
return redirect('/'.session()->get('lang'));
});
Route::group([
'prefix' => '{locale}',
'where'=>['locale'=>'[a-zA-Z]{2}']],
function() {
Route::get('/tour/{id}','HomeController#getTours');
});
});
in HomeContoller :
public function getTours($id){
dd($id);
}
when trying to access url : example.com/en/tour/5
getting result
en , but should be 5
Where is a problem and how to solve it?
Your route has 2 variables, {locale} and {id}, but your Controller method is only referencing one of them. You need to use both:
web.php:
Route::group(['prefix' => '{locale}'], function () {
...
Route::get('/tour/{id}', 'HomeController#getTours');
});
HomeController.php
public function getTours($locale, $id) {
dd($locale, $id); // 'en', 5
}
Note: The order of definition matters; {locale} (en) comes before {id} 5, so make sure you define them in the correct order.
I am trying Sentry for Laravel and I came accross with this problem. I have 2 group of routes:
Route::group(array( 'before' => 'Sentry|inGroup:Administrator'), function(){
Route::get('/', 'HomeController#index');
Route::resource('user', 'UserController');
});
Route::group(array( 'before' => 'Sentry|inGroup:Doctor'), function(){
Route::get('/', 'HomeController#index');
//Route::resource('user', 'UserController');
});
And my filters are:
Route::filter('inGroup', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
$group = Sentry::findGroupByName($value);
//dd($user->getGroups());
//dd($user->inGroup($group));
if( ! $user->inGroup($group)){
return Redirect::to('/login')->withErrors(array(Lang::get('user.noaccess')));
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('group.notfound')));
}
});
Route::filter('hasAccess', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
//dd($user); $user->hasAccess($value)
if( Sentry::check() ) {
//return Redirect::to('/')->withErrors(array(Lang::get('user.noaccess')));
return Redirect::to('/');
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}
});
The problem is the latter route with the 'Sentry|inGroup:Doctor' is firing. The filter is not getting the Administrator part or group.
How can I achieve to filter them based on the parameter that is passed on by the routes? Or, how can I make them dynamic?
You have defined twice the same "/" route, once in the first (Administrator's) group, and once in the second (Doctor's) group
Route::get('/', 'HomeController#index');
Since you haven't specified any prefix on either route group, the later (second) route overrides the first and the Administrator route cannot be reached.
You can try using prefixes:
// http://localhost/admin
Route::group(array('prefix' => 'admin', 'before' => 'Sentry|inGroup:Admins'), function()
{
Route::get('/', 'HomeController#index');
});
// http://localhost/doctors
Route::group(array('prefix' => 'doctors', 'before' => 'Sentry|inGroup:Doctor'), function()
{
Route::get('/', 'HomeController#index');
});
I'm using the group logic to filter the admin section of my website. I have a routing like this:
Route::group(array('before' => 'auth'), function() {
$datas['user']['email'] = Auth::user()->email;
Route::get('admin/dashboard', function() {
return View::make('admin/dashboard')->with(array('datas' => $datas));
});
//other routes...
});
How to make $datas available to all routes that are included in my group ?
You can share variables:
View::share('datas', $datas);
return View::make('admin/dashboard');
As you stated that you'd like to include $datas in every route, you can make use of the use keyword:
Route::group(array('before' => 'auth'), function()
{
$datas['user']['email'] = Auth::user()->email;
Route::get('admin/dashboard', function() use ($datas)
{
return View::make('admin/dashboard')->with(array('datas' => $datas));
});
});
You can learn about the use keyword here.
I am using the following code its from the laravel 4 site
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('user/{id}', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
the idea is to redirect subdomain.myapp.com to myapp.com/user/subdomain
.what I have is not working any suggestions?sorry I just started with laravel about a month now.
Remove user/{id} and replace it with / and then use the following url https://accountname.myapp.com and it will redirect to https://www.myapp.com/accountname
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account, $id) {
// ...
return Redirect::to('https://www.myapp.com'.'/'.$account);
});
});
Edit the answer to the correct answer
Route::group(array('domain' => '{account}.myapp.com'), function() {
Route::get('/', function($account) {
// ...
return Redirect::to('https://www.myapp.com/'.$account);
});
});
as Marc vd M answer but remove $id from get's closure function.
why use 'https://www.myapp.com'.'/'.$account and not 'https://www.myapp.com/'.$account