10

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?

Maeh
  • 1,774
  • 4
  • 18
  • 31
  • 2
    `ServiceA` has a `Client` and `ServiceB` has a `Client`. Without namespaces you cannot make this work other than naming them `ServiceAClient` and `ServiceBClient`. Now *that* makes your code unreadable and annoying. tl;dr; namespaces are a good thing. – Stan Feb 06 '15 at 23:18
  • 1
    using namespaces is a really good practice to keep your code organized – manix Feb 06 '15 at 23:21
  • 3
    I am aware of that, but it's not needed in the project I am working on. It complicates the code more than necessary... And being a Sublime Text user, which doesn't have autoimport, it really gets to be a pain – Maeh Feb 06 '15 at 23:22
  • I also found the forced namespacing annoying but I understand the benefits and good practice, so it's actually alright, but they put the namespace declaration on the first line, right after the opening tag. Now that's annoying. – Parziphal Mar 16 '15 at 03:10
  • Namspacing should be required for packages but should be optional for your core models. Either way no matter what your opinion is the question isn't should you have it's how can you disable. – Sabrina Leggett Dec 29 '16 at 19:34

2 Answers2

20

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

Community
  • 1
  • 1
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
  • 6
    Namespaces are great, enforcing monstrous Symfony-ish structure on a fresh project is not. Simplicity and lack of clutter were something heartwarming about L2-3-4, but 5 is out and there is no way back now. – pandasauce Feb 07 '15 at 23:28
  • @pandasauce I'm sad you feel that way. I added a little something on how to remove some of the namespacing done in Laravel 5. Take a look at the updated answer. – lukasgeiter Feb 07 '15 at 23:42
  • @Maeh And to you as well, I've updated the answer with some information on how to remove the namespaces. – lukasgeiter Feb 07 '15 at 23:44
  • 5
    *or search for another framework that follows **less good practices...*** +1 – Protomen Aug 10 '15 at 21:21
  • namespaces are good when you're developing packages and libraries. Not necessary for your core app code. People need to distinguish more between coding styles for packages. I see junior devs coming on who are so focused on using "namespaces" etc that the rest of their code sucks and isn't functional or maintainable. – Sabrina Leggett Jun 14 '16 at 15:22
2

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.

ddn
  • 1,360
  • 11
  • 8