AltoRouter-未定义的变量匹配?

时间:2019-02-15 16:34:23

标签: php routing altorouter

因此,我所有的主要路线都工作正常。但是,当我尝试使用动态路由并传递所选计算机的ID时,无法在我的 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]);
?>

这是我的文件夹结构: see here

0 个答案:

没有答案