Is there any way to configure Laravel 4 to use namespaces?

2

In Laravel 5 , I noticed that they have now added namespaces in the application folder. But things were not like that in the Laravel 4 version.

No Laravel 5 , for example Controllers would look like this:

namespace App\Http\Controllers;

class MeuController extends Controller {}

In% w_th of%, it would look like this:

class MeuController extends BaseController{}

But it's inside a straightforward folder structure, which would be able to implement Laravel 4 , according to the PSR standard.

For example, controllers are within namespace .

Is there any way to change the structure of app/controllers so that it accepts namespaces, as below?

namespace App\Controller;

class MeuController extends BaseController{}

This will be important to me, mainly to avoid class name conflict, as is the case with the Laravel 4 class, which prevents me from creating a modle called Log , as this would conflict. Everything would be solved with a simple namespace.

    
asked by anonymous 13.04.2016 / 14:12

1 answer

4

Composer and his magic

It is possible, but adding that to do this you should be accustomed to working with Composer .

Let's take the necessary steps:

1. Add the namespaces in each file of Laravel . If you do not want to take risks with your project, I suggest you use a copy to test first (make a new branch in git , for example).

You should add the namespaces for each folder.

For example:

The files of app/models will have the namespace App\Models . And so you will have to do for each folder, which use class, and you want to add the use of namespaces.

2. Open your composer.json file. Probably, it will have a snippet like this:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},

What we are going to do is this: All the folders where you want to use namespace , you will remove from this "list" above.

In this case I will remove app/controllers and app/models .

"autoload": {
    "classmap": [
        "app/commands",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},

Now, you will add the following snippet to your code

"autoload": {
    "classmap": [
        "app/commands",
        "app/traits",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],

    "psr-4": {
           "App\" : "app/",
     }
},

After changing your composer.json , you will now execute a command inside the root folder of your project (the location where composer.json is).

Execute: composer dump-autoload or composer dump .

After this, if you want to do a little test to see if the files were added to autoload , just do so:

php artisan tinker --env=local
> $model = new App\Models\MeuModel;

If all goes well, the model instance will be displayed on the screen.

Modifying the "Vendor Namespace" of your Project

PSR-4 small explanation

Note that in the composer configuration, we add the App\ excerpt. This is a requirement of Composer to generate autoload by PSR-4. In this case, App is the namespace base (the base name of the namespace), which is part of the requirement of the Psr-4 .

According to the PSR-4, this is called vendor namespace . That is, it is the first name of the namespace.

Are you confused? So I'll give you an example:

  namespace VendorNamespace\NomeDoPacote;

  class MinhaClasse {}

Therefore, it is not necessary for the vendor namespace to be exactly the name of the home directory that we are aiming to perform the autoload. But initially, as an example, let's do it just so you can understand.

Some people do not like using App as vendor namespace , as it is very common in some applications.

So if you want to rename the vendor namespace , you do not need to change the name of the app folder - as one would logically think, but you just need to change the setting made in composer .json.

For example: I do not want my namespace to be App\Models , but I want it to be Project\Models . That is, I want to change the vendor namespace App to Project .

How would I do this?

 "psr-4" : {
      "Project\" : "app/",
  }

After that, you will have to rotate composer dump again and change vendor namespace for each file.

If I then had a class named Remessa inside the app/models folders it would have to have this statement.

#app/models/Remessa.php

namespace Project\Models;

class Remessa extends \Eloquent {}

And the routes?

With the implementation of the namespaces in Laravel 4, when you have to use the definition of routes, things will change a little.

Before it was:

Route::get('/', ['uses' => 'HomeController@getIndex']);

Now it's:

Route::get('/',[
   'uses' => 'App\Controllers\HomeController@getIndex'
]);
    
13.04.2016 / 15:20