Support the ongoing development of Laravel.io →
posted 10 years ago
Architecture

Just started using Laravel 5 and having trouble understanding how the namespace works. Example for a standard controller:

<?php namespace App\Http\Controllers;

class WelcomeController extends Controller {

I have to add \ in front of the below functions or they are not recognized:

\Validator::make();
\Redirect::back();
\Request::all();

Can you point me in the right direction as to why this is required or if i'm not thinking about this correctly anymore?

Last updated 3 years ago.
0

Hi RMFogarty, That because when you are in specific namespace, here App\Http\Controllers, you can access directly on elements on others namespaces. If you want ton access on Validator, Lang ect you have to prefix it by . \ is the root namespace.

To remove the \ you can just include it with the use at the top of your class like the next example:


<?php namespace App\Http\Controllers;

use \Validator, \Redirect, \Request;

class WelcomeController extends Controller {
Validator::make();
Redirect::back();
Request::all();
Last updated 10 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

RMFogarty rmfogarty Joined 5 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.