Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Zavineo's avatar

Getting error in lesson In EPISODE 19 The PHP Practitioner: Forms, Request Types, and Routing

In EPISODE 19 The PHP Practitioner: Forms, Request Types, and Routing I get to 9:44 right after var_dump($router->routes); is added to routes.php. The array is dumped to the page as expected but, after the last item in the array I get the error: "Fatal error: Uncaught Exception: No route defined for this URI. in /home/lanebrun/public_html/core/Router.php:34 Stack trace: #0 /home/lanebrun/public_html/index.php(10): Router->direct('') #1 {main} thrown in /home/lanebrun/public_html/core/Router.php on line 34" I have deleted all code written in this episode, verified everything is working and followed the video 2 additional times and get the same results. What am I missing or over looking?

0 likes
8 replies
tomopongrac's avatar

can you post the code ...

maybe you did not defined route for post request

Zavineo's avatar

These pages were all changed in the episode or are referenced by another file.

index.php


require Router::load('routes.php')
    ->direct(Request::uri());

bootstrap.php


$app['config'] = require 'config.php';

require 'core/Router.php';
require 'core/Request.php';
require 'core/database/Connection.php';
require 'core/database/QueryBuilder.php';

$app['database'] = new QueryBuilder(
    Connection::make($app['config']['database'])

routes.php

$router->get('about', 'controllers/about.php');
$router->get('about/culture', 'controllers/about-culture.php');
$router->post('names', 'controllers/add-name.php');

var_dump($router->routes);

Router.php

{
    public $routes = [
        'GET' => [],
        'POST' => []
    ];

    public static function load($file)
    {
        $router = new static;

        require $file;

        return $router;
    }

    public function get($uri, $controller)
    {
        $this->routes['GET'][$uri] = $controller;
    }
    
    public function post($uri, $controller)
    {
        $this->routes['POST'][$uri] = $controller;
    }

    public function direct($uri)
    {
        if (array_key_exists($uri, $this->routes)) {
            return $this->routes[$uri];
        }
        throw new Exception($this->routes[$uri]);
    }
}

Request.php

{
    public static function uri()
    {
        return trim(
            parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'
        );
    }
}

index.view.php


<h1>Submit Your Name</h1>

<form method="POST" action="/names">
    <input name="name"></input>
    <button type="submit">Submit</button>
</form>

<?php require('partials/footer.php'); ?>
tomopongrac's avatar

did you try to complete the video because after jeffrey add requesType to direct method

Zavineo's avatar

I did try that before I posted and got what I thought was the same result. I just finished the video for the 2nd time and now the index page works, but when I use the form I get a similar error except now it is Router->direct('names', 'POST') instead of Router->direct(''). I'm going to review my code again and see if I can figure it out. Should I re-post my code for your review?

Zavineo's avatar

I'm frustrated so I'm posting the finished code from the video. So far I'm not seeing what is wrong. index.php


require Router::load('routes.php')
    ->direct(Request::uri(), Request::method());

bootstrap.php


$app['config'] = require 'config.php';

require 'core/Router.php';
require 'core/Request.php';
require 'core/database/Connection.php';
require 'core/database/QueryBuilder.php';

$app['database'] = new QueryBuilder(
    Connection::make($app['config']['database'])
);

routes.php

$router->get('about', 'controllers/about.php');
$router->get('about/culture', 'controllers/about-culture.php');
$router->get('names', 'controllers/add-name.php');

Router.php

{
    public $routes = [
        'GET' => [],
        'POST' => []
    ];

    public static function load($file)
    {
        $router = new static;

        require $file;

        return $router;
    }

    public function get($uri, $controller)
    {
        $this->routes['GET'][$uri] = $controller;
    }
    
    public function post($uri, $controller)
    {
        $this->routes['POST'][$uri] = $controller;
    }

    public function direct($uri, $requestType)
    {
        if (array_key_exists($uri, $this->routes[$requestType])) {
            return $this->routes[$requestType][$uri];
            var_dump($uri);
            var_dump($this->routes[$requestType]);
        }
        var_dump($uri);
        var_dump($this->routes[$requestType]);
        throw new Exception('No route defined for this URI.');
    }
}

Request.php

{
    public static function uri()
    {
        return trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
    }
    
    public static function method()
    {
        return $_SERVER['REQUEST_METHOD'];
    }
}

index.view.php


<h1>Submit Your Name</h1>

<form method="POST" action="names">
    <input name="name"></input>
    <button type="submit">Submit</button>
</form>

<?php require('partials/footer.php'); ?>
rollerdead's avatar

still got error? i still stuck because of this error on episode 19 too. if i'm using $router->define([]); = NO ERROR $router->get(); = ERROR "Fatal error: Uncaught Exception: No route defined for this URI........"

Please or to participate in this conversation.