2014-11-17 47 views
0

我使用Symfony 2.5(最新版本)創建一個Web應用程序。我在Windows 7上與WAMP本地服務器一起工作。在Symfony中設置正確的路由,以獲得正確的網址和路由

當我勞克的應用,這是URL我得到http://localhost/Symfony/web/。實際上我想補充/主頁

我有一個名爲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: /

所以,當我到達第一上的應用程序,我有我的首頁,所以它完美的作品。這是URL當我勞克應用程序:http://localhost/Symfony/web/

但是,如果我想添加這樣的路徑(我上的路徑行添加/主頁):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

我不太明白。 束被命名爲AdministrationBundle,這個控制器代碼: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 

有人可能會幫我解釋一下我爲什麼?

更新發布: 這個。htacess在Web文件夾

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> 
+0

您是否正確設置了虛擬主機? E.gl DocumentRoot /var/www/gir.dev/web 也是用正確的.htaccess啓用mod_rewrite? – Rooneyl

+0

我已更新我的帖子,所以你可以看到我的htaccess代碼。 –

+0

你的虛擬主機設置如何? – Rooneyl

回答

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

也是可行的soultion是隻把它定義在一個單一的路線:

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

這是同樣的問題。我改變我的代碼就像你建議我,但它仍然無法正常工作。 –

+1

@Julien你是否在開發環境?如果沒有,清除緩存。 'php app/console ca:c --env = prod' –

+0

我已經清除緩存,沒有任何變化。我相信我處於開發環境。 –

0

改變你的路線回到/主頁,檢查php app/console router:debug - 輸出,你會看到有對沒有路由/但是一個用於/主頁。

所以你需要另一個控制器/路由對從/到/主頁重定向。