【发布时间】:2015-08-03 23:24:27
【问题描述】:
我决定制作我自己的小型 PHP 框架,我将在现实生活中使用它,为社交应用程序创建 Web 服务。
我开始阅读 Fabien Potencier 的指南,在 Symfony 的组件之上创建自己的框架 - http://symfony.com/doc/current/create_framework/index.html。 我真的很喜欢他的 classLoader 和 http-foundation 库,并决定集成它们。
我阅读了整个教程,但我决定停止集成 Symfony 的组件,直到教程的第 5 部分,他得到了 Symfony http 内核、路由匹配器和控制器解析器(不包括那些)。
我的框架的前端控制器和路由映射器文件有问题。
front.php(前端控制器)
<?php
require 'config/config.php';
require 'config/routing.php';
require 'src/autoloader.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$request = Request::createFromGlobals();
$response = new Response();
$path = $request->getPathInfo();
if(isset($siteMap[$path])) {
call_user_func_array('src\Controllers' .'\\' . $siteMap[$path]['controller'] . '::' . $siteMap[$path]['action'], array($siteMap[$path]['arguments']));
} else {
$response->setStatusCode('404');
$response->setContent('Page not found');
}
$response->send();
?>
我的路由文件:
/*
Map of routes
*/
$siteMap = array('/' => array('controller' => 'IndexController', 'action' => 'indexAction', 'arguments' => ''),
'/categories' => array('controller' => 'CategoriesController', 'action' => 'indexAction', '
我现在想知道的是,如果没有进一步使用 Symfony 的组件,我该怎么做这样的事情:
在我的路由文件中,我想添加一个 URL,如 '/hello' 和 Controller - Hello Controller 和参数名称、年龄、性别,这将对应于浏览器中的请求 GET www.base/hello/samuel/11/male .
在 HelloController 中有一个 indexAction($name, $age, $gender) { ... }。我曾尝试查看 Symfony 的源代码,但到目前为止我还没有发现(我花了很多时间查看库的源代码)。我将进一步模块化和分离前端控制器和控制器的功能,但我想把它弄下来。
啊,欢迎任何关于进一步构建我的框架的建议(我正在创建一个类似 REST 的框架 - 需要可扩展、快速并可能每秒处理数万个请求的 Web 服务)。
【问题讨论】:
标签: php web-services rest symfony symfony-http-foundation