Problems with autoload

Asked

Viewed 142 times

3

am starting an application applying mvc. I created Composer.json with the following autoload parameters:

    "autoload":{
    "psr-4":{
        "SOCIAL\\":"vendor/SOCIAL/",
        "App\\":"App/",
        "Predis\\": "src/" 
        }

have in public the index that calls the route the following stretch:

require_once "../vendor/autoload.php";

if ( isset($_SESSION['id_user']) )
{
    $route = new \App\Route;
}
else
{
    header('location:'.$_SERVER['SERVER_NAME'].'/login.php');
}

I have in my route class the following:

namespace App;

class Route
{
    private $routes;

    public function __construct()
    {
        $this->initRoutes();
        $this->run($this->getUrl());

    }

    public function initRoutes()
    {
        $routes['home'] = array('route'=>'/','controller'=>'indexController','action'=>'index');
        $routes['login'] = array('route'=>'/login','controller'=>'indexController','action'=>'login');
        $this->setRoutes($routes);

    }

    public function setRoutes(array $routes)
    {
        $this->routes = $routes;

    }

    public function run($url)
    {
        array_walk($this->routes, function($route) use ($url)
            {
                if ( $url == $route['route'])
                {
                    $class = "App\\Controller\\".ucfirst($route['controller']);
                    $controller = new $class.'()';
                    $action = $route['action'];
                    $controller->$action();
                }
            });
    }

    public function getUrl()
    {
        return parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
    }
}

and within App Controllers Indexcontroller.php

namespace App\Controller;

class IndexController
{

    public function index()
    {
        echo "Route / Controller : Index";
    }

    public function login()
    {
        echo "Route / Controller : login";


    }

get the bug

Fatal error: Class 'App\Controller\IndexController' not found in /var/www/new/App/Route.php on line 36
  • where the autoload implementation is?

  • is in the index that calls the controller.

  • add it to the question

1 answer

0

The backslash is missing before the "App\\Controller\\".

$class = "\\App\\Controller\\".ucfirst($route['controller']);

Browser other questions tagged

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