将路由映射到Silex中文件系统上的目录结构

时间:2017-01-29 16:01:44

标签: routing server routes silex

我有这个目录层次结构:

htdocs
  |-- project
    |-- public
      |-- module
        |-- feature
          |-- index.php

示例 GET 请求

http://example.com/module/feature/1/email@server.com

服务器(Apache)如何知道我的目录是feature而不是1email@server.com

我是否需要在某处进行任何进一步的配置,或者Apache服务器是否为我开箱即用?

我是否还需要在Silex上配置任何路由?

1 个答案:

答案 0 :(得分:0)

  

服务器(Apache)如何知道我的目录是feature而不是1email@server.com

Silex有Routing System。您将路由传递给请求方法(此处为get),并以此方式捕获请求。

获取所有功能:

// htdocs/project/public/modules/features/index.php
$app->get('/modules/features', function (Application $app, Request $request) {

    $features = $app['em']->getRepository(Feature::class)->findAll();

    return $app['twig']->render('features/index.html.twig', array(
        'items' => $features,
    ));

});

获取功能编号#1:

// htdocs/project/public/modules/features/index.php
$app->get('/modules/features/{id}', function (Application $app, Request $request) {

    $id = $request->get('id');
    $feature = $app['em']->getRepository(Feature::class)->find($id);

    return $app['twig']->render('features/show.html.twig', array(
        'items' => $feature,
    ));

});

所以,那是,而不是 Apache 决定在哪个请求上返回什么。

  

我是否需要在某处进行任何进一步的配置,或者Apache服务器是否为我开箱即用?

     

我是否还需要在Silex上配置任何路由?

是的!您应该并且可以定义您的路线。请注意'/modules/features'& '/modules/features/{id}'部分在上面的摘要中。

URL路径与文件系统目录

URLs有一个名为Path的分层分类系统,File System也有类似的东西,尽管事实上它们是完全不同的东西。

它们并不是一直都是一样的,尽管它们可以作为一种简单的规则。

因此,您可以映射此网址:

http://example.com/modules/features

到这两个文件系统位置:

htdocs/project/public/modules/features/index.php

&安培;

htdocs/project/public/Controller/Frontend/modules/features/index.php

最后的注释

您最好利用目录结构最佳实践。例子是:

&安培; PHP Namespaces

相关问题