phplaravellaravel-4environment

App::environment not found with Laravel 4.1


I have a file in one of my packages (Oauth2 Madewithlove package), where I have a function that I am using. In this function, I want to use a certain Facebook App for my local environment and a certain for my production environment. Therefore the function looks like this:

public function search($query = '', $type = '') {

        if(App::environment('local')) {
            $url = 'https://graph.facebook.com/search?key=value&access_token=********|***************&q='.str_replace(' ', '+', $query).'&type='.$type;
        }
        else {
            $url = 'https://graph.facebook.com/search?key=value&access_token=***********|*****************&q='.str_replace(' ', '+', $query).'&type='.$type;
        }

        return json_decode(file_get_contents($url), true);
    }

Where the starred out portions are the app ids and secrets. However, when running this, I get the error:

Class 'OAuth2\Provider\App' not found

with the filepath:

/­vendor/­madewithlove/­laravel-oauth2/­src/­OAuth2/­Provider/­Facebook.php 

This points to the App:environment line. I have already added this to my config in the bootstrap/start.php file:

$env = $app->detectEnvironment(array(

    'local' => array('******-*******-MacBook-Pro.local'),

));

Any idea why I am getting the error? Thank you.


Solution

  • Use:

    \App::environment();
    

    Laravel is not namespaced, so every class will be in the root name space. Since your class Facebook is in the OAuth2\Provider namespace, it looks for OAuth2\Provider\App unless you specify that it is in the root namespace with the leading \.


    You can also fix this with use:

    <?php namespace OAuth2\Provider
    
    // Imports use the root namespace
    use App;
    
    class Facaebook
    {
    
        public function __construct()
        {
            var_dump(App::environment());
        }
    
    }