简体   繁体   English

具有Symfony组件的自定义框架的路由系统

[英]Routing System for Custom Framework with Symfony Components

在此处输入图片说明

My directory Structure is shown above. 我的目录结构如上所示。 I am trying to build a Framework with Symfony Components. 我正在尝试使用Symfony组件构建框架。 but one problem, when I hit a route that I defined, it doesn't give me back the response. 但是有一个问题,当我点击定义的路线时,它没有给我回响应。

Here is my index.php 这是我的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);

and my Core.php looks like this 和我的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;
    }
}

Does anyone know what the bug is? 有人知道错误是什么吗? What did I screw up? 我搞砸了什么?

As I mentioned in the comments, you're just missing the last little detail (->send()) where you tell the Response object to send the headers and echo the content. 正如我在评论中提到的那样,您只是缺少了最后一个小细节(-> send()),您在其中告诉Response对象发送标头并回显内容。 So: 所以:

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

And I think that should do it! 我认为应该这样做!

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM