How to use psr-4 in composer with different subfolders

1

I'm creating a test framework. I use composer to create the structure of my project. The composer.json file 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

Being that I'm having problems in the namespace.

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

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

For the controller / controller.php the code was:

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

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

Someone would know what's wrong with the psr4 setup.

    
asked by anonymous 03.04.2017 / 20:15

1 answer

0

There are some changes to make to your code.

The composer setting is correct. However, your PHP codes should be changed.

See your composer setup:

  

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

See its structure:

  

C: \ xampp \ htdocs \ webApp \ src \ app \ mvc \

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

  

C: \ xampp \ htdocs \ webApp \ src \ app \ mvc \ model \ model.php

So its correct namespace should be:

namespace App\model;

class Model {}

And you created it as:

namespace App;
class Model { }

The namespace call should also be successful. For example, within 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 class specific:

namespace App\controller;

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

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

A level of namespace was missing, which would be the model , controller and view folders.

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

Logo:

namespace App\model;

class Model {}

It should be:

  

C: \ xampp \ htdocs \ webApp \ src \ App \ mvc \ model \ Model.php

With the folder App and the file Model.php .

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

    
03.04.2017 / 20:31