Php Symfony';s路由组件(或我的误用)阻塞了我的应用程序
几天前,我问了一个问题,我如何为自己的php框架制作自己的参数解析器,我明白了答案的作用,但时间不允许我创建它,因此我决定使用Symfony的:Php Symfony';s路由组件(或我的误用)阻塞了我的应用程序,php,symfony,http,routing,benchmarking,Php,Symfony,Http,Routing,Benchmarking,几天前,我问了一个问题,我如何为自己的php框架制作自己的参数解析器,我明白了答案的作用,但时间不允许我创建它,因此我决定使用Symfony的: Symfony\Component\Routing\RouteCollection Symfony\Component\Routing\Route Symfony\Component\Routing\Matcher\UrlMatcher Symfony\HttpKernel\Controller\ControllerResolver Symfony\C
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个请求,是的,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
routing.php(所有路由)
AppKernel.php
名称空间核心\内核
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');
我的两个控制器都是空的,我不认为问题出在它们身上
另外,我正在学习Povertier的《如何创建您自己的框架》这听起来可能很愚蠢,但您是否尝试过改变路线顺序?我的意思是,先添加
/profiles
。我刚刚做了,什么都没有改变。你可以在浏览器中访问URL并给我们你得到的响应吗?这听起来可能很愚蠢,但你是否尝试过更改路由顺序?我的意思是,首先添加/profiles
。我刚刚添加了,什么都没有改变。你可以在浏览器中访问URL并给我们你得到的响应吗?
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);
}
}
}
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');