Php Symfony路由组件不是路由url
我有mvc php cms这样的文件夹结构:Php Symfony路由组件不是路由url,php,symfony,routing,symfony4,Php,Symfony,Routing,Symfony4,我有mvc php cms这样的文件夹结构: application ---admin --------controller --------model --------view --------language ---catalog --------controller ------------------IndexController.php --------model --------view --------language core --------controller.php //.
application
---admin
--------controller
--------model
--------view
--------language
---catalog
--------controller
------------------IndexController.php
--------model
--------view
--------language
core
--------controller.php
//...more
public
--------index.php
vendor
我使用composer json安装路由url以获取帮助:
{
"autoload": {
"psr-4": {"App\\": "application/"}
},
"require-dev":{
"symfony/routing" : "*"
}
}
现在,通过路由文档,我在index.php
中添加了路由代码:
require '../vendor/autoload.php';
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$route = new Route('/index', array('_controller' => 'App\Catalog\Controller\IndexController\index'));
$routes = new RouteCollection();
$routes->add('route_name', $route);
$context = new RequestContext('/');
$matcher = new UrlMatcher($routes, $context);
$parameters = $matcher->match('/index');
在我的IndexController中,我有:
namespace App\Catalog\Controller;
class IndexController {
public function __construct()
{
echo 'Construct';
}
public function index(){
echo'Im here';
}
}
现在,我在这个url中工作:localhost:8888/mvc/index
,并且看不到结果:imhere
IndexController
symfony路由url如何工作并在我的mvc结构中找到控制器?感谢您的练习和帮助。请求上下文应填充应用程序中的实际URI。您可以使用来自Syffon的HTTP基础包来填充这个:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
$context = new RequestContext();
$context->fromRequest(Request::createFromGlobals());
这里也有记录:
匹配($parameters=$matcher->match('/index');
)后,可以使用参数的\u controller
键实例化控制器并分派操作。我的建议是用不同的符号替换最后一个\
,以便于拆分,如App\Controller\Something::index
然后,您可以执行以下操作:
list($controllerClassName, $action) = explode($parameters['_controller']);
$controller = new $controllerClassName();
$controller->{$action}();
这应该与控制器类中的响应相呼应。我使用composer json添加了
“symfony/http foundation”:“*”
,并添加了代码,但我看不到反应!您应该使用composer require symfony/http foundation
添加它。您是否检查了软件包是否已实际安装?也许可以粘贴完整的例子。好吧,我想我知道还有什么发生了。你并不是真的在调度行动。我会更新我的答案。@NewCod3r我已经更新了应该解决您问题的说明。如果我是诚实的,我建议您只使用Symfony作为一个整体或类似于Laravel的东西,它提供了更方便的http相关操作的简单调度。