Система маршрутизации для Custom Framework с компонентами Symfony

0

Изображение 174551

Структура моего каталога показана выше. Я пытаюсь создать Framework с компонентами Symfony. но одна проблема, когда я нахожу маршрут, который я определил, он не возвращает мне ответ.

Вот мой index.php

<?php

$loader = require 'vendor/autoload.php';
$loader->register();

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

require 'lib/Framework/Core.php';
$request = Request::createFromGlobals();

// Our Framework is now handling itself the request
$app = new Framework\Core();

$app->map('/', function () {
    return new Response('This is the home page');
});

$app->map('/about', function () {
    return new Response('This is the about page');
});

$response = $app->handle($request);

и мой Core.php выглядит так

<?php namespace Framework;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface as HttpKernelInterface;

class Core implements HttpKernelInterface
{
    protected $routes = array();

    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
    {
        $path = $request->getPathInfo();

        // Does this URL match a route?
        if (array_key_exists($path, $this->routes)) {
            // execute the callback
            $controller = $this->routes[$path];
            $response = $controller();
        } else {
            // no route matched, this is a not found.
            $response = new Response('Not found!', Response::HTTP_NOT_FOUND);
        }

        return $response;
    }

    // Associates an URL with a callback function
    public function map($path, $controller) {
        $this->routes[$path] = $controller;
    }
}

Кто-нибудь знает, что такое ошибка? Что я испортил?

Теги:
oop
routing

1 ответ

1
Лучший ответ

Как я уже упоминал в комментариях, вы просто пропускаете последнюю небольшую деталь (-> send()), где вы указываете объекту Response для отправки заголовков и эхо-контента. Так:

$response = $app->handle($request);
$response->send();

И я думаю, что это нужно!

Ура!

Ещё вопросы

Сообщество Overcoder
Наверх
Меню