Laravel how to add group prefix parameter to route function - php

For example, I have defined routes like this:
$locale = Request::segment(1);
Route::group(array('prefix' => $locale), function()
{
Route::get('/about', ['as' => 'about', 'uses' => 'aboutController#index']);
}
I want to generate links for several locales (en, de, es,...). When I try to provide prefix parameter like this
$link = route('about',['prefix' => 'de']);
I got link like this example.com/en/about?prefix=de
How to provide prefix param to got link like this example.com/de/about

You can play around with something like this perhaps.
Route::group(['prefix' => '{locale}'], function () {
Route::get('about', ['as' => 'about', 'uses' => '....']);
});
route('about', 'en'); // http://yoursite/en/about
route('about', 'de'); // http://yoursite/de/about

You can do like this :
Route::group(['prefix'=>'de'],function(){
Route::get('/about', [
'as' => 'about',
'uses' => 'aboutController#index'
]);
});
Now route('about') will give link like this : example.com/de/about

Try this:
$locale = Request::segment(1);
Route::group(array('prefix' => $locale), function()
{
Route::get('/about', ['as' => 'about', 'uses' => 'aboutController#index']);
}
And while providing a link, you can use url helper function instead of route:
$link = url('de/about');
If you want more generic, use this in controller/view:
$link = url($locale.'/about');
where $locale could be en,de,etc

You can simply achieve it like as
Route::group(['prefix' => 'de'], function () {
Route::get('about', ['as' => 'de.about', 'uses' => 'aboutController#index']);
});
And you can use it like as
$link = route('de.about');

Related

Dynamic Route name Laravel 5.2

I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);

Launch 503 status for all routes with laravel routes.php file

I want launch a status code 503 for all my routes in Laravel 5.2.
I can not use the command 'php artisan down'.
So I want to do it manually in my file routes.php.
I try with this, but not working:
Route::any('/',function(){#this code not work for me
dd('not arrive here');
return abort(503);
});
This is the complete route.php file:
<?php
Route::any('/',function(){#this code not work for me
dd('not arrive here');
return abort(503);
});
// extra langs
$conf = Config::get('app.current_site_config');
$langs = [];
$langs[] = $conf['lng_default'];
if (!empty($conf['lng_extra']))
$langs = array_merge($conf['lng_extra'],$langs);
$prefix = false;
if (count($langs) > 1)
$prefix = true;
$pages = Config::get('app.web_config.lang_url');
foreach($langs as $lang)
{
foreach($pages as $key=>$value)
{
$key_underscored = str_replace('-','_',$key);
if ($prefix == false)
{
//echo $key_underscored.'_'.$lang.'<br>';
Route::any('/'.$value[$lang],['as' => $key_underscored.'_'.$lang, 'uses' => 'WebController#'.$key_underscored ]);
Route::get('/'.$value[$lang].'/{seo_name}/{id}',['as' =>$key_underscored.'_seo_'.$lang, 'uses' => 'WebController#'.$key_underscored]);
Route::get('/404-error', ['as' => 'error404'.'_'.$lang, 'uses' => 'WebController#error404']);
Route::get('/500-error', ['as' => 'error500'.'_'.$lang, 'uses' => 'WebController#error500']);
Route::post('shipping-info-post', ['as' => 'shipping_info_post'.'_'.$lang, 'uses' => 'WebController#shipping_info_post']);
Route::post('payment-post', ['as' => 'payment_post'.'_'.$lang, 'uses' => 'WebController#payment_post']);
Route::post('cart-post', ['as' => 'cart_post'.'_'.$lang, 'uses' => 'WebController#cart_post']);
}
else
{
//echo '/'.$lang.'/'.$value[$lang] . ' as '.$key_underscored.'_'.$lang.' uses '. 'WebController#'.$key_underscored.'<br>';
Route::any('/'.$lang.'/'.$value[$lang],['as' => $key_underscored.'_'.$lang, 'uses' => 'WebController#'.$key_underscored]);
Route::get('/'.$lang.'/'.$value[$lang].'/{seo_name}/{id}',['as' =>$key_underscored.'_seo_'.$lang, 'uses' => 'WebController#'.$key_underscored]);
// generic (not optimized for SEO)
Route::get('/'.$lang.'/404-error', ['as' => 'error404_'.$lang, 'uses' => 'WebController#error404']);
Route::get('/'.$lang.'/500-error', ['as' => 'error500_'.$lang, 'uses' => 'WebController#error500']);
Route::post('/'.$lang.'/shipping-info-post', ['as' => 'shipping_info_post_'.$lang, 'uses' => 'WebController#shipping_info_post']);
Route::post('/'.$lang.'/payment-post', ['as' => 'payment_post_'.$lang, 'uses' => 'WebController#payment_post']);
Route::post('/'.$lang.'/cart-post', ['as' => 'cart_post_'.$lang, 'uses' => 'WebController#cart_post']);
}
}
}
if (count($langs) > 1)
{
Route::get('/',function(){
header('Location: '.route('index_'.Config::get('app.locale')).'/');
exit;
});
}
else
Route::get('/', ['as' => 'index_'.$langs[0], 'uses' => 'WebController#index']);
How I can launch the 503 state above all routes of my site?
Just add an empty file called down in the following folder and you're good to go : /storage/framework/.
That's basically what's php artisan down does in /vendor/laravel/framework/src/Illuminate/Foundation/Console/DownCommand.php :)
public function handle($request, Closure $next)
{
return response()->view('503',[], 500);
}
we must create 503.blade.php page in view directory to handle request.
Create a middleware that return a 503 and apply it as a group to your routes.

Laravel Is it possible to pass a variable in route group?

I have following routes:
Route::group(['prefix' => 'group1'], function () {
Route::get('view1', ['as' => 'group1_view1', 'uses' => 'group1Controller#get_view1']);
Route::get('view2', ['as' => 'group1_view2', 'uses' => 'group1Controller#get_view2']);
});
Route::group(['prefix' => 'group2'], function () {
Route::get('view1', ['as' => 'group2_view1', 'uses' => 'group2Controller#get_view1']);
Route::get('view2', ['as' => 'group2_view2', 'uses' => 'group2Controller#get_view2']);
});
I wish to pass variables, for example, $title = 'group-one' to all views in group1 and $title = 'group-two' to all views in group2. Instead of adding variable $title in all methods in each group controller, is it possible to pass variables groupwise in routes?
NO you cannot pass variables into route groups, but if in case you need one piece of view/code in multiple views, title for example, you can use view composers instead.

How to use route in Laravel 5.2 with suffix

I try
$suffix = '.test';
Route::get('article/edit/{name?}'.$suffix, ['uses' => 'ArticleController#edit', 'as' => 'Edit']);
and it works well. Then I add in auth group, $name always returns with $suffix (for example: $name = 'abc.test')
Route::group(['middleware' => 'auth'], function () {
global $suffix;
Route::get('article/edit/{name?}'.$suffix, ['uses' => 'ArticleController#edit', 'as' => 'Edit']);
});
But I want it returns $name = 'abc';
Where am I wrong?
What if you do?
Route::group(['middleware' => 'auth'], function () {
global $suffix = '.test';
Route::get('article/edit/{name?}'.$suffix, [
'uses' => 'ArticleController#edit',
'as' => 'Edit'
])->where('name', '/[^a-zA-Z0-9_\.-]/');
});

How to use Redirect::intended in laravel?

I want the route to bypass authentication filter after successfully login. So I use Redirect::intended. But it makes problem. Here is my code
In LoginController#doLogin
if (Auth::attempt($credentials)) {
return Redirect::intended("home.index");
}
In Routes
Route::group(array('before' => 'auth'), function() {
Route::resource('home', 'HomeController');
Route::get('/', 'HomeController');
});
My routes
If I put the the home resource route without the auth filter, then it will work. (Not redirect to login route.).That code is given below
Route::resource('home', 'HomeController');
Route::group(array('before' => 'auth'), function() {
Route::get('/', 'HomeController');
}); /* No Problem with this code */
But I want to work with auth filter.I'm using laravel 4.
Please help me...
Currently I use this:
in filter.php
Route::filter('sentry', function() {
if (!Sentry::check())
{
return Redirect::route('authLogin');
}
});
so in route.php
Route::get('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::post('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::group(array('before' => 'sentry'), function() {
Route::get('user/', array('as' => 'user', 'uses' => 'UserController#index'));
Route::get('user/index', array('as' => 'userIndex', 'uses' => 'UserController#index'));
});
I preffer named routes.

Categories