【问题标题】:set correctly routing in Symfony to have correct url and route在 Symfony 中正确设置路由以获得正确的 url 和路由
【发布时间】:2014-11-17 16:13:29
【问题描述】:

我使用 Symfony 2.5(最新版本)来创建一个 Web 应用程序。我使用 Windows 7 上的 WAMP 本地服务器

当我启动应用程序时,这是我得到http://localhost/Symfony/web/url。事实上,我想添加 /homepage

我的第一个包名为 WelcomeBundle 和他的控制器:它涉及我的应用程序主页Gir/WelcomeBundle/Controller/HomePageController.php

<?php

namespace Gir\WelcomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomePageController extends Controller
{
    public function indexAction()
    {
        return $this->render('GirWelcomeBundle:HomePage:index.html.twig');
    }
}

这是路由文件Gir/WelcomeBundle/Ressource/config/routing.yml

GirWelcomeBundle_HomePage:
  pattern:  /
  defaults: { _controller: GirWelcomeBundle:HomePage:index }
  requirements:
    methods: GET
    schemes:  https

这是通用路由文件app/config/routing.yml

GirWelcomeBundle:
    resource: "@GirWelcomeBundle/Resources/config/routing.yml"
    prefix:   /

gir_administration:
    resource: "@GirAdministrationBundle/Resources/config/routing.yml"
    prefix:   /

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

gir_user:
    resource: "@GirUserBundle/Resources/config/routing.yml"
    prefix:   /

所以当我首先进入应用程序时,我就有了我的主页,所以它运行良好。这是我启动应用程序时的 urlhttp://localhost/Symfony/web/

但如果我想像这样添加路径(我在路径行添加/homepage):Gir/WelcomeBundle/Ressource/config/routing.yml

GirWelcomeBundle_HomePage:
  pattern:  /homepage
  defaults: { _controller: GirWelcomeBundle:HomePage:index }
  requirements:
    methods: GET
    schemes:  https

应用程序没有找到路径,我有这个错误:

No route found for "GET /" (from "http://localhost/Symfony/")
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »

有人知道为什么吗?

然后,当我尝试启动管理页面时,浏览器显示该页面是一个无法访问的网页,例如this

我真的不明白。 该包名为 AdministrationBundlecontroller 代码:Gir/AdministrationBundle/Controller/AdministrationController.php

<?php

namespace Gir\AdministrationBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class AdministrationController extends Controller
{
    public function indexAction()
    {
        return $this->render('GirAdministrationBundle:Admin:index.html.twig');
    }
}

这是路由文件Gir/AdministrationBundle/Ressource/config/routing.yml

gir_administration_homepage:
    pattern:  /administration
    defaults: { _controller: GirAdministrationBundle:Administration:index }
    requirements:
    methods: GET
    schemes:  https

有人可以帮我解释一下为什么吗?

更新帖子: 这是 web 文件夹中的 .htacess

DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On


    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app_dev.php [QSA,L]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

【问题讨论】:

  • 您是否正确设置了虚拟主机?例如 DocumentRoot /var/www/gir.dev/web 是否使用正确的 .htaccess 启用了 mod_rewrite?
  • 我已经更新了我的帖子,所以你可以看到我的 htaccess 代码。
  • 你的虚拟主机设置怎么样?
  • @Rooneyl,我还没有创建虚拟主机,它是如何工作的?

标签: php .htaccess symfony url routing


【解决方案1】:

根据你的描述,我猜你的 routing.yml 看起来像这样:

GirWelcomeBundle_HomePage:
    pattern:  /
    defaults: { _controller: GirWelcomeBundle:HomePage:index }
    requirements:
        methods: GET
        schemes:  https

GirWelcomeBundle_HomePage:
    pattern:  /homepage
    defaults: { _controller: GirWelcomeBundle:HomePage:index }
    requirements:
        methods: GET
        schemes:  https

这种方法的问题是你的GirWelcomeBundle_HomePage.解决方法很简单: 以不同的方式命名路由,例如:GirWelcomeBundle_RootGirWelcomeBundle_Home

另外可行的方法是在一条路线中定义它:

GirWelcomeBundle_HomePage:
    pattern:  /{homepage}
    defaults: { _controller: GirWelcomeBundle:HomePage:index, homepage: 'homepage' }
    requirements:
        methods: GET
        schemes:  https

【讨论】:

  • 同样的问题。我按照您的建议更改了我的代码,但它仍然无法正常工作。
  • @Julien 你在开发环境中吗?如果没有,请清除缓存。 php app/console ca:c --env=prod
  • 我已经清除了缓存,没有任何变化。而且我确信我处于开发环境中。
【解决方案2】:

把你的路由改回/homepage,检查php app/console router:debug-output,你会发现没有/只有一个/homepage的路由。

所以你需要另一个控制器/路由对来从 / 重定向到 /homepage。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多