5

Working with Laravel 5 for the first time. I understand the use of namespaces and why you need to use them. What I don't understand is why I need to add use statements like the following (at the top of a controller):

use Session;
use Input;
use Response;

I have to do this so that I don't get fatal exceptions like:

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

Maybe I'm missing something?? These classes seem to be part of the framework, and were by default in L4.x.

Can anyone help me understand why this is so in L5 or how I can avoid having to do this with L5?

The Googles have failed me on this one.

Joel Leger
  • 349
  • 4
  • 11

1 Answers1

5

The process of using use statements in files is called aliasing or importing. http://php.net/manual/en/language.namespaces.importing.php gives a good explanation of it, but in short:

  • If you reference a class using a 'relative' or just the class's name(Such as Session), PHP is going to check the current namespace for the existence of that class.
  • Laravel 4 controllers were not technically in a namespace. They were part of the root namespace of the application, and therefor a reference to another class in the root namespace (Session) would find just that. Laravel 5 controllers are namespaced in App\Http\Controllers\.
  • The only way to solve this is to tell PHP the full namespace of a given class, such as in this case \Session, as these classes are added as part of the root namespace. An example would be something like new \App\MyCompany\Models\SomeModel();
  • The "use" statements at the top of a class file allow you to alias these full class-paths as anything you want. use \App\MyCompany\Models\SomeModel as Foo would allow you to do new Foo() and get an instance of \App\MyCompany\Models\SomeModel. (Most would leave off the 'as' statement, and just reference new SomeModel())
  • There is no way, nor need to try, to avoid adding these use statements to your classes when trying to leverage classes that are in a different namespace from the current class. It is a useful construct of the language, and not a limitation.

Hope that makes sense.

V13Axel
  • 838
  • 5
  • 10
  • Thanks it does make sense. Very similar to Use statements in C#. However in C# you can "use System" or "use System.Web" and it gives you access to a number of classes etc you can refer to. It would seem like if these classes are all in the root namespace, you would be able to just do something like "use App" and they would be automatically included. But that appears to not be the case? – Joel Leger Mar 12 '15 at 03:55
  • You cannot import an entire namespace in PHP. You can, however, alias a namespace from its full path name to just the lowest level, and then reference it from there. To continue my earlier example, `use \App\MyCompany\Models`. and then later you can do `new Models\SomeModel()` – V13Axel Mar 12 '15 at 12:19
  • Just a tip - when referring to a class in the root namespace, you can reference it with just a slash, i.e. - \DB::enableQueryLog(); Unless I'm using a root class extensively in a class, I prefer not to waste a line for a "use" statement. I do, however, prefer to refer to the root class rather than use a helper function based on a root class, i.e. return \Response::json() over just return json(). Though the former is more verbose, it is also more clear to anyone else looking at the code. – Ixalmida Jun 25 '15 at 21:26