Php 超级简单框架301从Symfony路由重定向

Php 超级简单框架301从Symfony路由重定向,php,symfony,symfony-routing,Php,Symfony,Symfony Routing,我尝试构建超级简单的框架。我不想使用CONTROLLERS。。。基本上一切都很好,这是一个很好的开始,为简单的网站,但我想要一个更多的功能,我有问题 我试图实现尽可能简单的解决方案,添加路由页面301重定向。例如,我想要一个301到的/facebook 这是我的密码。。。index.php如下: <?php require_once __DIR__.'/../vendor/autoload.php'; $dotenv = new Dotenv\Dotenv(__DIR__.'/../'

我尝试构建超级简单的框架。我不想使用CONTROLLERS。。。基本上一切都很好,这是一个很好的开始,为简单的网站,但我想要一个更多的功能,我有问题

我试图实现尽可能简单的解决方案,添加路由页面301重定向。例如,我想要一个301到的/facebook

这是我的密码。。。index.php如下:

<?php

require_once __DIR__.'/../vendor/autoload.php';

$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
$dotenv->load();

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

$request = Request::createFromGlobals();
$routes = include __DIR__.'/../routes/web.php';

$context = new Routing\RequestContext();
$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);

try {
    extract($matcher->match($request->getPathInfo()), EXTR_SKIP);
    ob_start();
    include sprintf(__DIR__.'/../resources/views/%s.php', $_route);

    $response = new Response(ob_get_clean());
} catch (Routing\Exception\ResourceNotFoundException $e) {
    $response = new Response();
    $response->setStatusCode(404);
    include __DIR__.'/../resources/views/errors/404.php';
} catch (Exception $e) {
    $response = new Response();
    $response->setStatusCode(500);
    include __DIR__.'/../resources/views/errors/500.php';
}

$response->send();

在您的情况下,您可以执行以下操作:

//routes.php

use Symfony\Component\HttpFoundation\RedirectResponse;

$permanentRedirect = function ($url) {
    return new RedirectResponse($url, 301);
};

//...

$routes->add('facebook', new Routing\Route('/facebook', array(
    '_controller' => $permanentRedirect,
    'url' => 'http://facebook.com/example',
)));

//...

return $routes;
接下来,在
index.php中:

//...

try {
    extract($matcher->match($request->getPathInfo()), EXTR_SKIP);

    if (isset($_controller) && isset($url)) {
        $response = call_user_func($_controller, $url);
    } else {
        ob_start();
        include sprintf(__DIR__.'/../resources/views/%s.php', $_route);

        $response = new Response(ob_get_clean());
    }        
} catch (Routing\Exception\ResourceNotFoundException $e) {
    //...
} catch (Exception $e) {
    //...
}

symfony/frameworkbundle这是一个大软件包。我只有symfony/httpfoundation和symfony/routing一个就可以了,但是所有这些来自symfony/frameworkbundle的东西都不是一个好主意。顺便说一句,它能与index.php中的try方法一起工作吗?我不这么认为。是的,这基本上适用于任何可调用函数。警告:include(/home/vagrant/Web/example.com/public/。/resources/views/twitter.php):无法打开流:第22行的/home/vagrant/Web/example.com/public/index.php中没有这样的文件或目录。我已经实现了您的代码(确切地说是class::method)。但是我从前面的评论中得到了这个错误。@Yonel没有Symfony框架,
path
默认值不起作用吗?我这样问是因为在路由器组件上有一个指向“如何重定向…”的链接。
//...

try {
    extract($matcher->match($request->getPathInfo()), EXTR_SKIP);

    if (isset($_controller) && isset($url)) {
        $response = call_user_func($_controller, $url);
    } else {
        ob_start();
        include sprintf(__DIR__.'/../resources/views/%s.php', $_route);

        $response = new Response(ob_get_clean());
    }        
} catch (Routing\Exception\ResourceNotFoundException $e) {
    //...
} catch (Exception $e) {
    //...
}