Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 namespaces

I just downloaded Laravel 5 and started migrating to it. However, I find the required use of namespaces really annoying.

I don't feel like I am getting much from it, other than cluttering my code.

How can I disable the namespacing requirement?

like image 390
Maeh Avatar asked Feb 06 '15 23:02

Maeh


Video Answer


2 Answers

I don't think you should disable or remove namespaces. The main reason for namespacing is to avoid conflicts with classes that have the same name. As soon as an application gets larger you will have classes that have the same name. Example from the Framework source:

Illuminate\Console\Application and Illuminate\Foundation\Application

Both are called the same. Only because of the namespacing you can import the right class. Of course you could also name them:

ConsoleApplication and FoundationApplication

But while the namespace normally is only used when importing a class at the top of a file:

use `Illuminate\Console\Application`

The name itself is used everywhere in the code. That's something that really clutters up your code, too long class names.

Besides the naming thing, namespaces also encourage better structure and help with knowing where your files are. That's because Laravel's default structure is PSR-4 compliant. That means if you have a controller App\Http\Controllers\HomeController you can be certain that you will find a HomeController.php under app/Http/Controllers.

I am aware of that, but it's not needed in the project I am working on.

Maybe it doesn't make sense for the current project but getting used to namespaces will help you tackle bigger projects in the future

And being a Sublime Text user, which doesn't have autoimport, it really gets to be a pain

I don't know Sublime Text that well, but CodeIntel might have auto import. Otherwise consider switching to another editor / IDE. I can highly recommend JetBrains PhpStorm


In the end, if you still don't want to use namespaces, keep using Laravel 4 or search for another framework that follows less good practices...


Removing namespaces from your app classes

While a totally don't recommend this, it is possible to at least remove some of the namespacing in your application.

For example the default controller namespace App\Http\Controllers can be changed to no namespace at all in RouteServiceProvider:

protected $namespace = '';

And for your models you can just remove the namespace in the file and your good. But keep in mind that without namespaces PSR-4 autoloading won't work anymore. You will have to autoload your files using classmap in composer.json

like image 174
lukasgeiter Avatar answered Oct 23 '22 02:10

lukasgeiter


You can avoid using namespaces for own classes by defining them in the global namespace in your composer.json file. Like this:

"autoload": {
    "psr-0": {
    "": ["app/Http/Controllers/",
        "app/models/",
        "app/helpers"
        ]
},

You will also have to change your app/Providers/RouteServiceProvider.php to:

protected $namespace = '';

for routing to work.

like image 29
ddn Avatar answered Oct 23 '22 01:10

ddn