0

I am using Laravel 4.

in app/config/prod/app.php

I have an array of things, laid out like this,

<?php

return array(

‘debug’ => “fault”

// and more ...

)

I want to set my default value to a different thing base on auth user type.

IF the user type is admin, I want to set the debug to true ELSE set it to false.

Can I do an if-else check within an array in PHP ?

I’ve tried using ternary operator, but no luck. I hope I didn’t do anything wrong.

isset( Auth::user()->type == “Admin” ) ? ‘debug’ => “true” : ‘debug’
 => “fault”

Thanks for your help in advance ! :)

1
  • 1
    This has nothing to do with Laravel. Learn PHP. Commented Dec 15, 2014 at 21:52

2 Answers 2

1

What you can generally do is use the ternary operator for the value and not the full key value pair

'debug' => (Auth::user()->type == "Admin" ? true : false)

However, I'm not sure what is available when this config gets loaded (Auth::user() is probably not). Let me know if this works and I'll come back to you.

Update

A little test just confirmed my assumption. The Auth class is not available when config files get parsed. That means you will have to set the value afterwards. I suggest you leave the default to 'debug' => false and use this if the user is admin:

if(Auth::user()->type == 'Admin'){
    Config::set('app.debug', true)
}

You can do this wherever you want as long as it's "early enough". For example you can use the global App::before (in filters.php)

App::before(function($request){
    if(Auth::check() && Auth::user()->type == 'Admin'){
        Config::set('app.debug', true)
    }
});
Sign up to request clarification or add additional context in comments.

13 Comments

Where exactly should I put them ? I tried both of your suggestion. I got this. It's doesn't show any debug tips as it should.
Put the App::before in app/filters.php. And take a look at app/storage/logs/laravel.log there you can see your error message.
I did that, and it didn't work :(. Also, why do I need to see the log for ? Thanks :)
For the error message? the screenshot you posted is because debugging is turned off (app/config/app.php) you could also turn it on temporarily
My goal is to turn that on all the time as long as the Auth::user()->type == "Admin", I don't think, it's matter because it's me. :)
|
0

You could create a new filter and then apply it to the routes you want to check.

Route::filter('check_admin', function()
{
    if( Auth::user()->type == 'Admin' ){
       Config::set('app.debug',true)
    }
});

Now, you can use your filter like this:

Route::get('my-url', ['before'=>'check_admin'],function(){
    // Do your route logic here
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.