How to use psr-4 in the Composer with different subfolders

Asked

Viewed 550 times

1

I am creating a test framework. I use Composer to create the structure of my project. The file Composer.json looks like this:

{
   "autoload": {
      "psr-4": {
         "App\\": "src/app/mvc/"
      }
   }
}

I divided my project with the following structure:

C: xampp htdocs webapp src app mvc\

In mvc I have the following subfolders: controller; model; view

Since I’m having trouble with namespace.

For example for the /model/model.php the code was left:

<?php
   namespace App;
   class Model
   {
      public function getText($str = 'Olá mundo!')
      {
         return $str;
      }
   }

For the controller/controller.php the code was left:

<?php
   namespace App\controller;
   class Controller
   {
      public function index()
      {
        $model = new model\Model;
        $view = new view\View;
        $view->render($model->getText());
      }
   }

php is returning an error in the.php controller Fatal error: Class 'App controller model Model' not found in C: xampp htdocs webapp src app mvc controller Controller.php on line 7

Would anyone know what is wrong with configuring psr4.

  • Try this new \App\model\Model;

  • @Guilhermebirth with this modification in the controller.php file when trying to instantiate the model gives the following error: Fatal error: Class 'App controller app model Model' not found in C: xampp htdocs webapp src app mvc controller Controller.php on line 7

  • Is Model in the same folder as Controller? It doesn’t make much sense this error, you put the bar in front that I suggested? Thus new \App\model\Model; and nay thus new App\model\Model;

1 answer

0


There are some modifications to perform in your code.

The Composer configuration is correct. However, your PHP codes should be changed.

See your Composer configuration:

"App ": "src/app/mvc/"

See its structure:

C: xampp htdocs webapp src app mvc\

Soon after, you created the class Model the file model.php, resulting in the following path:

C: xampp htdocs webapp src app mvc model model.php

Therefore, his correct namespace should be:

namespace App\model;

class Model {}

And you raised him as:

namespace App;
class Model { }

The namespace call must also be right. For example, inside the controller namespace, it can be done in two ways:

namespace App\controller;

class Controller
{
   public function index()
   {
     $model = new \App\model\Model;
   }
}

Or

namespace App\controller;

use \App\model; //é interpretado como "use \App\model as model"

class Controller
{
   public function index()
   {
     $model = new model\Model;
   }
}

or, in particular, the class:

namespace App\controller;

use \App\model\Model; //é interpretado como "use \App\model\model as Model"

class Controller
{
   public function index()
   {
     $model = new Model;
   }
}

It lacked a level of namespace, which would be the folders model, controller and view.

Another important detail, in windows the file system is case-insensitive, whereas in linux it is case-sensitive. Therefore, your file system should have the same case as the namespaces and classes.

Soon:

namespace App\model;

class Model {}

Must be:

C: xampp htdocs webapp src App mvc model Model.php

With the briefcase App and the file Model.php.

But this is only valid for Linux systems, in your case is Windows even (will give you headache deploy later).

  • what kind of headache you refer to at the time of deploy?

  • 1

    For example, you are developing in Windows and have created class Model in the archive model.php and, in your environment, everything will work perfectly. By deploying to a Linux environment and running the code, the class Model will not be found, because the Composer will search for the archive Model.php. In Windows it doesn’t matter, in Linux it does. That’s one of the reasons PHP development is encouraged in Linux.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.