Laravel FrozenNode Administrator run in maintenance mode - laravel

My question is: How can I leave the Frozennode administrator runs normaly on Laravel Maintenance Mode?
This is what I got in global.php
App::down(function()
{
return Response::view('maintenance', array(), 503);
});
Thanks!

I've dug in the core, there's no way you can do it. Laravel checks for a file named down in app/storage/meta folder, if it's there, Laravel won't even call the routes, it'll just show the error page.
This is isDownForMaintenance function from laravel:
public function isDownForMaintenance()
{
return file_exists($this['path.storage'].'/meta/down');
}
There's no configuration possible.
An alternative way to the laravelish "maintenance mode" is to set a new value in config/app.php, add:
'maintenance' => true,
Then add this to your before filter:
App::before(function($request)
{
if(
Config::get('app.maintenance') &&
Request::segment(1) != 'admin' // This will work for all admin routes
// Other exception URLs
)
return Response::make(View::make('hello'), 503);
});
And then just set :
'maintenance' => true,
To go back to normal mode

There is actually another way, more straightforward. As you can read in Laravel documentation, returning NULL from closure will make Laravel ignore particular request:
If the Closure passed to the down method returns NULL, maintenance mode will be ignored for that request.
So for routes beginning with admin, you can do something like this:
App::down(function()
{
// requests beginning with 'admin' will bypass 'down' mode
if(Request::is('admin*')) {
return null;
}
// all other routes: default 'down' response
return Response::view('maintenance', array(), 503);
});

Related

Auth facade doesn't work without Sanctum api as middleware in Laravel 8

I'm creating an api through which anybody can view a page, however only admin can see all posts, while users are restricted to approved only. This is implemented via is_verified boolean variable where admin is given value of 1 and user the value of 0. I want to create a function like this
public function show(){
if(Auth::check()){
$showAllDetails = Events::all();
echo $showAllDetails;
}else {
$showUserDetails = Events:all()->where('is_verified',1);
echo $showUserDetails;
}
}
However Auth:check only works if I have sanctum api in my route
Route::middleware('auth:sanctum')->group(function () {
Route::get('view', [ViewController::class, 'show']);
});
If I run this code on Hoppscotch, it only shows if the admin is logged in (User don't require login). So a user can't see any post. If I remove the auth:sanctum middleware, only the else part of the code runs and no auth check or any stuff can run .
I need a way to incorporate both in a single function so that I can create a single route instead of creating two routes for different persons. Any way of doing such things?
public function show(){
if(Auth::check()){
$showAllDetails = Events::all();
echo $showAllDetails;
}else {
$showUserDetails = Events::where('is_verified',1)->get();
echo $showUserDetails;
}
}
I guess your else part is incorrect query, change your else part like above

Session does not save in first request in laravel 5.2

My situation is weird.
I enclosed my routes in Route::group(['middleware' => ['web']], function () { /* Routes */ });
I save my session using
Session::put('customer_id', $customer->id);
But when I refresh my browser. The session is gone. Then I save it again then refresh and works fine. It does not works in first save.
I'm checking it using
if (Session::has('customer_id)) {
// Session saved.
} else {
// Session not saved.
}
I also tried middlewareGroup but doesn't work.
Does your $middlewareGroups array in app/Http/Kernel.php include the following line?
\Illuminate\Session\Middleware\StartSession::class
It would explain why the session isn't working.
The other thing to check is if the session is being overwritten elsewhere in your code. I.e. are you calling Session::put, anywhere else, and would $customer->id ever be null / false / 0?

Echoing the domain I'm 301 redirecting in Laravel 5.3

I'm redirecting a secondary domain in my routes:
Route::group(['domain' => 'http://olddomain.com'], function() {
Route::get('/', function() {
return Redirect::to('http://newdomain.com/', 301);
});
});
I want to be able to add an alert to a page that was redirected from olddomain.com. My solution is to use an if statement but I don't know how to check if the user has entered olddomain.com if there's a redirect happening.
Is there any way to echo olddomain.com if that's how the user is trying to access the site?
Have you tried to flash it to a session? Once you detect the first request, you should be able to add some information to the session. Once you redirect, you can check to see if this information exists in the session, and if it does, perform whatever action you need.
To start, you should always point your routes to an actual controller. It doesn't take much work, and later on it will allow you to take advantage of Laravel's route caching. Assuming you are using a Controller, your code might look like this.
public function oldRoute(){
Session::flash('old_route', \Request::url());
// Perform any additional logic
return Redirect::to('http://newdomain.com/', 301);
}
public function newRoute(){
if(!empty(Session::get('old_route'){
// You know the user came from the old route
}
// Return your view
}
Because session data are not preserved accross different domains, the simplier approach would be to pass a hashed parameter into your url before you redirect to the new domain.
On your new domain you can detect the url parameter, decrypt it and do your stuff accordingly.
Routes redirect cross-domain with hashed token parameter
// Your secret key can be anything you want
$secretKey = 'oldlaravel.dev';
Route::group(['domain' => 'oldlaravel.dev'], function() use ($secretKey) {
Route::get('/', function() use ($secretKey) {
$hashedToken = Hash::make($secretKey);
return Redirect::to("http://newlaravel.dev?token=$hashedToken", 301);
});
});
Route::group(['domain' => 'newlaravel.dev'], function() use ($secretKey) {
Route::get('/', function() use ($secretKey) {
if (Request::get('token')) {
if (Hash::check($secretKey, Request::get('token'))) {
echo 'You got here by redirect from my old domain!';
} else {
echo 'Who the hell are you?!'; //came here with an invalid token parameter
}
} else {
echo 'You got here directly!';
}
});
});

changing url, and locale on the fly with laravel

My code in laravel to handle multiple language is:
$languages = array('it-IT','en-GB','fr-FR');
$lingua = Request::segment(1);
if(in_array($lingua, $languages)){
\App::setLocale($lingua);
}else{
$lingua = 'it-IT';
}
Route::group(array('prefix' => $lingua), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'ItemController#menu'));
Route::get('/{idcampo}','ItemController#show');
});
How can i:
1)Make the page start always with it-IT as default. (i need it because I use $lingua to fetch from a database) so i can't have that null. Should I use a redirect::to / to /it-IT?
2) change url and language(app:locale) on he fly with a link in the upper section of every pages. withouth returning to the home.
3) to link pages I learn to use:
URL::route('home')
but how to do it when the link change with the entry of a database (for example my link is {{ URL::to($lingua. '/'. $campo[1].'/') }}) I need to use
URL::action('ItemController#show', ($lingua. '/'. $campo[1].'/'))
EDIT:
OK at the top of my pages there is a link to change language on the fly.
Italian //
English //
French
I create a controller clled LanguageController
<?php
class LanguageController extends BaseController {
public function select($lingua)
{
// Store the current language in the session
Session::put('lingua', $lingua);
return Redirect::back(); // redirect to the same page, nothing changes, just the language
}
}
I create a route:
Route::get('lingua/{lingua}', 'LanguageController#select');
Route::get('/', array('as' => 'home', 'uses' => 'ItemController#menu'));
Route::get('/mondo/','ItemController#mondo');
Route::get('/{idcampo}','ItemController#show');
I have my ItemController#menu
public function menu()
{ $linguadefault='it-IT';
$lingua = Session::get('lingua',$linguadefault);
$data = DB::table('campo')->lists('id');
return View::make('index')->with('campo',$data)->with('lingua',$lingua);
}
1) I don't understand why i need to route at lingua/{lingua} if i never route there but i use a url:action to a controller directly.
2) now i need to add
$linguadefault='it-IT';
$lingua = Session::get('lingua',$linguadefault);
at the beginning of every function to have a lingua variable in my page right?
3) now my language seems stucked to french and i can't change it anymore.
I would not use the language in the URL all the time, you can just switch languages when you need and persist it:
1) Use Session to persist the language chosen:
// Set the default language to the current user language
// If user is not logged, defaults to Italian
$linguaDefault = Auth::check()
? Auth::user()->lingua
: 'it-IT';
/// If not stored in Session, current language will be the default one
\App::setLocale(Session::get('lingua', $linguaDefault));
To have the language always set in your application, you can put this code in your file
app/start/global.php
And you don't need to add this anywhere else. So it will use it in this order:
a) Language stored in Session (selected online)
b) Language user has in database
c) Italian
2) To change the language you create a route:
Route::get('lingua/{lang}', 'LanguageController#select');
Your links
URL::action('LanguageController#select', 'it-IT')
URL::action('LanguageController#select', 'en-GB')
URL::action('LanguageController#select', 'fr-FR');
And in your controller you just have to do:
public function select($lang)
{
// Store the current language in the session
Session::put('lingua', $lang);
return Redirect::back(); // redirect to the same page, nothing changes, just the language
}
3) This way you don't need your language in all your URLs, you don't have to deal with it in all your routes. If your user changes the language in database, you just:
$user->save();
Session::put('lingua', $user->lingua);
return Redirect::route('home'); // or anything else

Laravel route acts craizy

Hy i have this http://laravel.io/bin/jaPB
The problem is when i go to:
domain.com -> it serves the homepage (wich is ok)
domain.com/foo -> it serves a subpage (still ok)
but when i go from one of those to:
domain.com/en -> it gives an error (not ok)
But after hitting refresh its ok.
So again when i'm on domain.com/en first time error after refresh ok
Same goes to subpage like domain.com/en/contact first time error after refresh ok
I would point out that the error says first time it tries to go to PublicPageController#subpage
but this shouldn't happen when i go to domain.com/en it should need to go to PublicPageController#homepage
Any idea ?
Thank you all.
My guess here form looking at the way you set up the locale-based routes is that the Session::get('urilang) isn't set the first time you visit, hence the error, and is only set once you've been to a page first.
Now, I haven't yet had to deal with multilingual sites, but as far as I'm aware the way you're doing it is not the correct way. Instead think of the lang key as a URI parameter, and use the filter to validate and set it for the routes. Something a bit like the below code:
// Main and subpage - not default language
Route::group(array('prefix' => '{lang}', 'before' => 'detectLanguage'), function () {
Route::get('', 'PublicPage#homepage');
Route::get('{slug}', 'PublicPage#subpage');
});
// Main and subpage - default language
Route::group(array('before' => 'setDefaultLanguage'), function () {
Route::get('/', 'PublicPage#homepage');
Route::get('/{slug}', 'PublicPage#subpage');
});
Route::filter('detectLanguage', function($route, $request, $response, $value){
// hopefully we could do something here with our named route parameter "lang" - not really on sure the details though
// set default
$locale = 'hu';
$lang = '';
// The 'en' -> would come from db and if there is more i would of corse use in array
if (Request::segment(1) == 'en')
{
$lang = 'en';
$locale = 'en';
}
App::setLocale($locale);
Session::put('uriLang', $lang);
Session::put('locale', $locale);
});
Route::filter('setDefaultLanguage', function($route, $request, $response, $value){
App::setLocale('hu');
Session::put('uriLang', '');
Session::put('locale', 'hu');
});
I don't know if you can use a segment variable in a Route::group prefix, but you should certainly have a go at it as it'd be the most useful.
That said, I wouldn't advise setting up default language routes that mimic specific language routes but without the language segment. If I were you, I'd set up a special root route that will redirect to /{defaultlang}/ just so you have fewer routing issues.

Resources