I'm trying to learn how to use alto router and what I want i'ts "pretty simple".
exemple:
- "/" should call "AppController->index()"
- "/profil" should call "ProfilController->profil()"
- /profil/1" should call "ProfilController->profilById()
etc...
This is what I've tried so far:
<?php
use App\Controller\AppController;
require './vendor/autoload.php';
putenv("BASE_URL=/formulaire-php");
// Router
$router = new AltoRouter();
$router->setBasePath('/formulaire-php');
$router->map('GET', '/', 'AppController#index');
$match = $router->match();
if ($match === false) {
echo "404";
} else {
list($controller, $action) = explode('#', $match['target']);
if (is_callable(array($controller, $action))) {
call_user_func_array(array($controller,$action), array($match['params']));
} else {
// here your routes are wrong.
// Throw an exception in debug, send a 500 error in production
}
}
htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
composer.json
{
"require": {
"altorouter/altorouter": "^2.0"
},
"autoload": {
"psr-4": {
"App\\": "src/controller/"
}
}
}
AppController:
namespace App\Controller;
class AppController
{
public function index()
{
echo "I index code + return index view here";
}
for now I have no error at all so it's difficult to know what's going on..