Rémi LCD
Rémi LCD

Reputation: 21

How can make my router (AltoRouter) work with my controllers on a PHP app

I would like to know how make my controllers be queried before sending back the view. More simply, I would like to make my router work with the MVC architecture. Currenly, i managed to configure my routes and send the user to the views with the AltoRouter library and without controller.
Index.php

<?php
require 'vendor/altorouter/altorouter/AltoRouter.php';
$router = new AltoRouter();

// map homepage
$router->map('GET', '/', function () {
    require __DIR__ . '/views/home.php';
});

$router->map('GET', '/error404', function () {
    require __DIR__ . '/views/error404.php';
});

// match current request url
$match = $router->match();

// call closure or throw 404 status
if (is_array($match) && is_callable($match['target'])) {
    call_user_func_array($match['target'], $match['params']);
} else {
// no route was matched
    header('location: /error404');
}

Htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Controller

<?php
require 'Models/Home.php'
class homeController
{
    public function __construct()
    {
        $this->main();
    }

    public function main()
    {
        $_model = new Home();
    }
}
$controler = new homeController()

Upvotes: 0

Views: 147

Answers (0)

Related Questions