Symfony的路由组件(或我的滥用)使我的应用程序窒息

时间:2015-08-06 19:40:28

标签: php symfony http routing benchmarking

几天前,我问了一个问题,我怎么能为自己的php框架制作自己的论证解析器,我理解为什么感谢答案,但时间不允许我创建它所以我决定推出Symfony&#39 ; s:

Symfony\Component\Routing\RouteCollection
Symfony\Component\Routing\Route
Symfony\Component\Routing\Matcher\UrlMatcher
Symfony\HttpKernel\Controller\ControllerResolver
Symfony\Component\HttpKernel

如果Silex,Drupal和Laravel可以使用它们,为什么不使用它?

我实现了它们并创建了一个新的路由,让它调用它/ testroute(TestController :: IndexAction),到目前为止它还可以。 我用apache的ab进行了测试,用于我的索引路线' /' :对10个并发用户的1000个请求,每秒产生3400个请求。

但后来我测试了我的新路线,结果是......每秒9个请求,是九。

我无法摆脱这种感觉,这是我的错,但我愿意接受建议,这是我的框架的代码:

的.htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /framework/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ front.php [L,QSA]
</IfModule>
DirectoryIndex index.html front.php

front.php(我的前台控制器)

要求&#39; config / configuration.php&#39;;

require 'autoloader.php';

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

use core\Kernel\AppKernel;


$request = Request::createFromGlobals();
$routes = include 'config/routing.php';

$context = new RequestContext();
$context->fromRequest($request);

//App kernel created by me
$appKern = new AppKernel($request, $routes, $context);

$appKern->run();

routing.php(所有路线)

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

$routes = new RouteCollection();

$routes->add('index', new Route('/', array('_controller' => 'app\Controllers\IndexController::indexAction')));
$routes->add('profiles', new Route('/profiles', array('_controller' => 'app\Controllers\ProfilesController::indexAction')));


return $routes;

AppKernel.php

namespace core \ Kernel;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel;
use Symfony\Component\Routing;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;

class AppKernel {

    private $request;

    private $routes;

    private $context;


    public function __construct(Request $request, RouteCollection $routes, RequestContext $context) {

        $this->request = $request;
        $this->routes = $routes;
        $this->context = $context;
    }

    private function callController($controller, $arguments) {

        $response = call_user_func_array($controller, $arguments);
    }

    public function run() {

        $matcher = new Routing\Matcher\UrlMatcher($this->routes, $this->context);
        $resolver = new HttpKernel\Controller\ControllerResolver();

        try {
            $this->request->attributes->add($matcher->match($this->request->getPathInfo()));

            $controller = $resolver->getController($this->request);
            $arguments = $resolver->getArguments($this->request, $controller);

            $this->callController($controller, $arguments);
        } catch (Routing\Exception\ResourceNotFoundException $e) {
            $response = new Response('Not Found', 404);
        } catch (Exception $e) {
            $response = new Response('An error occurred', 500);
        }
    }

}

autoloader.php

require_once 'Symfony/Component/ClassLoader/ClassLoader.php';

use Symfony\Component\ClassLoader\ClassLoader;

$loader = new ClassLoader();

// to enable searching the include path (eg. for PEAR packages)
$loader->setUseIncludePath(true);

// ... register namespaces and prefixes here - see below

$loader->register();

$loader->addPrefix('Symfony\Component\HttpFoundation', 'Symfony/Components/http-foundation');
$loader->addPrefix('app\Controllers', ROOT . 'app/Controllers');
$loader->addPrefix('core\Db', ROOT . 'core/Db');
$loader->addPrefix('core\Kernel', ROOT . 'core/Kernel');
$loader->addPrefix('Symfony\Component\Routing', 'Symfony/Component/Routing');
$loader->addPrefix('Symfony\Component\HttpKernel', 'Symfony/Component/HttpKernel');

我的两个控制器都是空的,我不相信问题出在他们身上。

P.S。我正在关注Potencier如何创建自己的框架

0 个答案:

没有答案