Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 56 lines (37 sloc) 1.71 KB

Namespacing in Controllers

Laravel class facades are readily available for use in your routes file. For example, the following would work:

Route::get('/example', function () {
    return App::environment();

In this example, we've used Laravel's App facade to return the current environment.

If we tried to use this same statement (return App::environment()) in a controller, however, it would fail. To quickly test this out, add the App::environment line to your BookController's index method:

public function index() {    
    return App::environment(); # <- This is what we're testing out
    #return 'Here are all the books...';

Testing the above action would show this error:

Class not found

The reason the App::environment() statement works in the web.php routes file is because that file is in the global namespace.

When you move into the controller, you're then in that controller's namespace, which is App\Http\Controllers.

The error message makes this clear—

Class 'App\Http\Controllers\App' not found

To fix this add a use statement at the top of your controller to import the App class:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App; # <- Add this

class BookController extends Controller

    public function index()
        return App::environment(); # Now it should work!
        #return 'Here are all the books...';


When using a Laravel class facade in a controller file, a use statement is needed to “import” that class into the controller's namespace.

You can’t perform that action at this time.