How to use route in Laravel 5.2 with suffix - php

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_\.-]/');
});

Related

Laravel - Error "Unable to prepare route [/] for serialization. Uses Closure."

I'm new to Laravel and now i manage project that was left by someone.
i try to add a function to API, what i edit :
1) Add a Method :
myproject/app/Http/Controllers/Api/ArticleController.php
2) Add Routes to controler :
myprojectmyproject/routes/api.php
However, when i try to run php artisan route:cache i got below error :
Route cache cleared! \n
LogicException : Unable to prepare route [/] for serialization. Uses Closure.
my route file, myproject/routes/api.php :
Route::group (['prefix' => 'v1', 'middleware' => 'ValidateHeaderSignature'], function() {
Route::group(['prefix' => 'auth'], function() {
Route::post('/login', 'Api\AuthController#login');
Route::post('/register', 'Api\AuthController#register');
Route::post('/login-social-media', 'Api\AuthController#loginSocialMedia');
Route::post('/forgot-password', 'Api\AuthController#forgotPassword');
Route::group(['middleware' => 'jwt.auth'], function() {
Route::patch('/change-password', 'Api\AuthController#changePassword');
Route::post('/logout', 'Api\AuthController#logout');
});
});
Route::group(['prefix' => 'foundation-donate'], function() {
Route::get('/', 'Api\FoundationDonateController#index');
});
Route::group(['prefix' => 'greeting-chat'], function() {
Route::get('/', 'Api\GreetingChatController#index');
});
Route::group(['prefix' => 'prayer-time'], function () {
Route::get('/', 'Api\PrayerTimeController#index');
Route::get('/montly', 'Api\PrayerTimeController#getMontlyPrayerTimes');
});
Route::group(['prefix' => 'asmaul-husna'], function () {
Route::get('/', 'Api\AsmaulHusnaController#index');
});
Route::group(['prefix' => 'guidance'], function () {
Route::get('/zikir', 'Api\GuidanceController#zikirGuidances');
Route::get('/prayer', 'Api\GuidanceController#prayerGuidances');
});
Route::group(['prefix' => 'duas'], function () {
Route::get('/', 'Api\DuasController#index');
Route::get('/index', 'Api\DuasController#index');
Route::get('/all', 'Api\DuasController#allPrayers');
Route::get('/category/{category}', 'Api\DuasController#category');
Route::get('/show/{id}', 'Api\DuasController#show');
});
Route::group(['prefix' => 'zakat'], function () {
Route::get('/', 'Api\ZakatController#index');
Route::get('/index', 'Api\ZakatController#index');
Route::get('/all', 'Api\ZakatController#allPrayers');
Route::get('/category/{category}', 'Api\ZakatController#category');
Route::get('/show/{id}', 'Api\ZakatController#show');
});
Route::group(['prefix' => 'playlist'], function () {
Route::get('/zikir', 'Api\PlaylistSongController#playlistZikir');
Route::get('/shalawat', 'Api\PlaylistSongController#playlistShalawat');
Route::get('/duas', 'Api\PlaylistSongController#playlistDuas');
Route::get('/murottal', 'Api\PlaylistSongController#playlistMurottal');
Route::get('/songs', 'Api\PlaylistSongController#playlistSongs');
});
Route::group(['prefix' => 'dzikir'], function() {
Route::get('/primary', 'Api\DzikirController#primaryDzikir');
Route::get('/my-dzikir', 'Api\DzikirController#myDzikir');
Route::get('/categories', 'Api\DzikirController#dzikirCategories');
Route::group(['middleware' => 'jwt.auth'], function() {
Route::get('/point-total', 'Api\DzikirController#pointTotal');
Route::get('/histories', 'Api\DzikirController#histories');
Route::get('/total-dzikir-history', 'Api\DzikirController#totalDzikirHistory');
Route::post('/post-dzikir', 'Api\DzikirController#postDzikir');
});
});
Route::group(['prefix' => 'sadaqah'], function() {
Route::group(['middleware' => 'jwt.auth'], function() {
Route::get('/histories', 'Api\DzikirController#sadaqahHistories');
});
});
Route::group(['prefix' => 'article'], function() {
Route::get('/', 'Api\ArticleController#index');
Route::get('/daily-reflection', 'Api\ArticleController#getDailyReflection');
Route::get('/get-random', 'Api\ArticleController#getRandom');
});
Route::group(['prefix' => 'notification'], function() {
Route::get('/quote', 'Api\NotificationController#prayerQuotes');
});
Route::group(['prefix' => 'user', 'middleware' => 'jwt.auth'], function() {
Route::get('/show', 'Api\UserController#show');
Route::patch('/update-profile', 'Api\UserController#update');
});
Route::group(['prefix' => 'master'], function() {
Route::get('/location', 'Api\MasterController#location');
});
});
if i got error because of Uses Closure, why previous developer can populate route?
by run php artisan route:list i can see list of route that have ever made before.
any idea ?
===Update, Add routes/web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes(['register' => false]);
Route::get('/home', 'HomeController#index')->name('home');
Route::get('register/activation/{code}', 'Auth\\RegisterController#activation')->name('register.activation');
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function() {
Route::get('/user-apps/list-index', ['as' => 'user-apps.list-index', 'uses' => 'Admin\\UserAppsController#listIndex']);
Route::get('/user-apps/resend-confirmation', ['as' => 'user-apps.resend-confirmation', 'uses' => 'Admin\\UserAppsController#resendConfirmation']);
Route::resource('/user-apps', 'Admin\\UserAppsController');
Route::get('/user/list-index', ['as' => 'user.list-index', 'uses' => 'Admin\\UserController#listIndex']);
Route::resource('/user', 'Admin\\UserController');
Route::get('/dzikir-playlist-category/list-index', ['as' => 'dzikir-playlist-category.list-index', 'uses' => 'Admin\\DzikirPlaylistCategoryController#listIndex']);
Route::resource('/dzikir-playlist-category', 'Admin\\DzikirPlaylistCategoryController');
Route::get('/dzikir-playlist/list-index', ['as' => 'dzikir-playlist.list-index', 'uses' => 'Admin\\DzikirPlaylistController#listIndex']);
Route::resource('/dzikir-playlist', 'Admin\\DzikirPlaylistController');
Route::get('/dzikir-playlist-homepage/list-index', ['as' => 'dzikir-playlist-homepage.list-index', 'uses' => 'Admin\\DzikirPlaylistHomepageController#listIndex']);
Route::resource('/dzikir-playlist-homepage', 'Admin\\DzikirPlaylistHomepageController');
Route::get('/dzikir-playlist-my-zikir/list-index', ['as' => 'dzikir-playlist-my-zikir.list-index', 'uses' => 'Admin\\DzikirPlaylistMyZikirController#listIndex']);
Route::resource('/dzikir-playlist-my-zikir', 'Admin\\DzikirPlaylistMyZikirController');
Route::get('/greeting-chat/list-index', ['as' => 'greeting-chat.list-index', 'uses' => 'Admin\\GreetingChatController#listIndex']);
Route::resource('/greeting-chat', 'Admin\\GreetingChatController');
Route::get('/foundation-donate/list-index', ['as' => 'foundation-donate.list-index', 'uses' => 'Admin\\FoundationDonateController#listIndex']);
Route::resource('/foundation-donate', 'Admin\\FoundationDonateController');
Route::get('/asmaul-husna/list-index', ['as' => 'asmaul-husna.list-index', 'uses' => 'Admin\\AsmaulHusnaController#listIndex']);
Route::resource('/asmaul-husna', 'Admin\\AsmaulHusnaController');
Route::get('/guidance/list-index', ['as' => 'guidance.list-index', 'uses' => 'Admin\\GuidanceController#listIndex']);
Route::resource('/guidance', 'Admin\\GuidanceController');
Route::get('/content-category/list-index', ['as' => 'content-category.list-index', 'uses' => 'Admin\\ContentCategoryController#listIndex']);
Route::resource('/content-category', 'Admin\\ContentCategoryController');
Route::get('/duas/list-index', ['as' => 'duas.list-index', 'uses' => 'Admin\\DuasController#listIndex']);
Route::resource('/duas', 'Admin\\DuasController');
Route::get('/zakat/list-index', ['as' => 'zakat.list-index', 'uses' => 'Admin\\ZakatController#listIndex']);
Route::resource('/zakat', 'Admin\\ZakatController');
Route::get('/quote/list-index', ['as' => 'quote.list-index', 'uses' => 'Admin\\QuoteController#listIndex']);
Route::resource('/quote', 'Admin\\QuoteController');
Route::get('/playlist-song-category/list-index', ['as' => 'playlist-song-category.list-index', 'uses' => 'Admin\\PlaylistSongCategoryController#listIndex']);
Route::resource('/playlist-song-category', 'Admin\\PlaylistSongCategoryController');
Route::get('/playlist-song/list-index', ['as' => 'playlist-song.list-index', 'uses' => 'Admin\\PlaylistSongController#listIndex']);
Route::resource('/playlist-song', 'Admin\\PlaylistSongController');
Route::get('/album/list-index', ['as' => 'album.list-index', 'uses' => 'Admin\\AlbumController#listIndex']);
Route::resource('/album', 'Admin\\AlbumController');
Route::get('/artist/list-index', ['as' => 'artist.list-index', 'uses' => 'Admin\\ArtistController#listIndex']);
Route::resource('/artist', 'Admin\\ArtistController');
Route::get('/article/list-index', ['as' => 'article.list-index', 'uses' => 'Admin\\ArticleController#listIndex']);
Route::resource('/article', 'Admin\\ArticleController');
});
When you want to use route caching, you can't use closure to register routes in any file.
As you still have the default route from the fresh Laravel installation in your routes/web.php file, you get this error because when you do php artisan route:cache, Laravel under the hood selializes the routes files and combine them in a single one as he lookup would be quicker.
To solve the issue, you can simply remove the route if that is unneeded or move it to a controller like you did for all the others routes.
The error should be gone then.
Simply delete any route with a callback function like the default routes.
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
edit in
Route::middleware('auth:api')->get('/user', 'SomeController#someMethod');

Laravel Route [dashboard] is not defined

Trying to play with Laravel today for the first time. I am getting the following error when I attempt to visit:
InvalidArgumentException
Route [dashboard] not defined.
routes/web.php
Route::get('/', ['as' => '/', 'uses' => 'LoginController#getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginController#postLogin']);
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginController#getLogout']);
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController#dashboard']);
});
LoginController.php
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web'))
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt([
'username' => $request->username,
'password' => $request->password,
'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
as like name(). You should use one of the two :
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', 'DashboardController#dashboard')->name('dashboard');
});
Or
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', [
'as' => 'dashboard',
'uses' => 'DashboardController#dashboard']);
});
After, you clear route cache with php artisan route:clear
Final, you can use php artisan route:list to lists all routes and the action bind
Try this:
Route::get('/dashboard','DashboardController#dashboard')->name('dashboard');
When you use as it names the route , so you have add two name dashbbaorddashboard because you use as and name
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/dashboard', ['uses' => 'DashboardController#dashboard'])->name('dashboard');
});
This will work

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 how to add group prefix parameter to route function

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');

Laravel multiple roles on same route

This are currently my route implementation for user and auth roles on Laravel 5.1:
Route::group(['prefix' => 'admin', 'middleware' => 'auth:administrator'], function()
{
$a = 'admin.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'AdminController#getHome']);
});
Route::group(['prefix' => 'user', 'middleware' => 'auth:user'], function()
{
$a = 'user.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController#getHome']);
});
I have another role where user can signup as merchant, but the issue is, how can I implement merchant route without duplicating the code, since both user and merchant using similar dashboard where merchant have extra features.
The implementation that currently worked is:
Route::group(['prefix' => 'user', 'middleware' => 'auth:merchant'], function()
{
$a = 'user.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController#getHome']);
});
Thanks!!
You should be able to pass on a list of middlewares to your route using an array.
Route::group(['prefix' => 'user', 'middleware' => ['auth:user', 'auth:merchant']], function()
{
$a = 'user.';
Route::get('/', ['as' => $a . 'home', 'uses' => 'UserController#getHome']);
});
However, I am not sure if this yields a result which does what you hope to achieve. Perhaps all this does is only allowing the route to users which belong to roles "user" AND "merchant", which probably isn't what you intend to do.

Categories