AltoRouter - не определено соответствие переменной? - PullRequest
0 голосов
/ 15 февраля 2019

Итак, все мои основные маршруты работают просто отлично.Но когда я пытаюсь использовать динамические маршруты и передать идентификатор выбранной машины, я не могу получить к нему доступ в моем machine_details.php с помощью $ match ['params'] ['id'].Это мой index.php файл:

<?php
require_once 'vendor/autoload.php';

// Setup for base path of project
$router = new AltoRouter();
$router->setBasePath('/caffemania');

// Main routes
$router->map('GET', '/', function() { require __DIR__ . '/landing.php'; }, 'login');
$router->map('GET', '/pocetna', function() { require __DIR__ . '/homepage.php'; }, 'pocetna');
$router->map('GET', '/lokacije', function() { require __DIR__ . '/locations.php'; }, 'locations');
$router->map('GET', '/aparati', function() { require __DIR__ . '/machines.php'; }, 'machines');
$router->map('GET', '/izvjestaji', function() { require __DIR__ . '/reports.php'; }, 'reports');
$router->map('GET', '/skladiste', function() { require __DIR__ . '/warehouse.php'; }, 'warehouse');

// Specific routes
$router->map('GET', '/aparati/[i:id]', function($id) { require __DIR__ . '/machine_details.php'; }, 'machine-details');

// Get the current match
$match = $router->match();

// Call closure or throw 404 status
if ($match && is_callable($match['target'])) {
    call_user_func_array($match['target'], $match['params']); 
} else {
    // No route was matched
    header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
?>

Это machine_details.php:

<?php
require_once '../../../Users/Korisnik/vendor/autoload.php';
include 'models/Machine.php';
include 'config/Database.php';

$loader = new Twig_Loader_Filesystem('templates/');
$twig = new Twig_Environment($loader, [
    'cache' => 'cache/',
]);
$template = $twig->load('machine_details.html');

// Instantiate DB & Connect
$database = new Database();
$db = $database->connect();

// Create a new instance of machine and read its details
$machine = new Machine($db);
$machine->id = $match['params']['id'];
$machine->readSingle();

echo $template->render(['page_name' => 'Stanje aparata', 'machine' => $machine]);
?>

Это моя структура папок: см. Здесь

...