Problems with autoload

3

I'm starting an application by applying mvc. I created composer.json with the following autoload parameters:

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

I have in public the index that calls the route the following excerpt:

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

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

I have the following route class in my route class:

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";


    }

I get the error

Fatal error: Class 'App\Controller\IndexController' not found in /var/www/new/App/Route.php on line 36
    
asked by anonymous 12.10.2016 / 19:22

1 answer

0

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

$class = "\App\Controller\".ucfirst($route['controller']);
    
13.10.2016 / 03:11