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