liorchamla / pratique-symfony-routing

Cours pratique sur le composant symfony/routing
2 stars 4 forks source link

Les paramètres de routes: passages de la variables $resultat #1

Open b-cassine opened 3 years ago

b-cassine commented 3 years ago

Bonjour Lior,

voilà mon problème. Quand je lance url : http://127.0.0.1:3000/show/100 j'ai ce retour : Notice: Undefined variable: resultat in /home/zapa/Documents/workspace/www/pratique-symfony-routing/pages/show.php on line 15

je ne comprends pas pourquoi ... Si tu as une explication je suis preneur merci.

page index.php

`<?php

use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection;

require DIR . '/vendor/autoload.php';

$routeList = new Route('/'); $routeCreate = new Route('/create'); $routeShow = new Route('/show/{id}');

$collection = new RouteCollection(); $collection->add('list', $routeList); $collection->add('show', $routeShow); $collection->add('create', $routeCreate);

$matcher = new UrlMatcher($collection, new RequestContext());

$pathInfo = $_SERVER['PATH_INFO'] ?? '/';

try { $resulat = $matcher->match($pathInfo);

$page = $resulat['_route'];
require_once "pages/$page.php";  

} catch (ResourceNotFoundException $e) { require 'pages/404.php'; return;

}`

page show.php

`<?php

$data = require_once "data.php"; $id = $resultat['id'];

if (!$id || !array_key_exists($id, $data)) { throw new Exception("La tâche demandée n'existe pas !"); }

// Si tout va bien, on récupère la tâche correspondante et on affiche $task = $data[$id];`

liorchamla commented 3 years ago

Ta variable est déclarée sous le nom $resulat .. Manque un T comme $resulTat :)

b-cassine commented 3 years ago

Honte sur moi :( mais au moins j'ai bien choisi mon pseudo. Merci Lior !