2016-12-04 109 views
1

我有一個區域設置路由的問題。Symfony3自動區域設置路由

www.example.com/ - >不工作(沒有路由發現 「GET /」)

www.example.com/en/ - >工作

在我來說這應該一樣。英語是默認語言,如果www.example.com被調用,我想用英文提供頁面。 當我調用沒有區域設置的頁面時,我總是收到一個錯誤(找不到「GET /」的路由)。

的routing.yml

app: 
resource: "@AppBundle/Controller/" 
type: annotation 
prefix: /{_locale} 

config.yml

imports: 
 
    - { resource: parameters.yml } 
 
    - { resource: security.yml } 
 
    - { resource: services.yml } 
 

 
# Put parameters here that don't need to change on each machine where the app is deployed 
 
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration 
 
parameters: 
 
    locale: en 
 

 
framework: 
 
    #esi:    ~ 
 
    translator:  { fallbacks: ["%locale%"] } 
 
    secret:   "%secret%" 
 
    router: 
 
     resource: "%kernel.root_dir%/config/routing.yml" 
 
     strict_requirements: ~ 
 
    form:   ~ 
 
    csrf_protection: ~ 
 
    validation:  { enable_annotations: true } 
 
    #serializer:  { enable_annotations: true } 
 
    templating: 
 
     engines: ['twig'] 
 
    default_locale: "%locale%" 
 
    trusted_hosts: ~ 
 
    trusted_proxies: ~ 
 
    session: 
 
     # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id 
 
     handler_id: session.handler.native_file 
 
     save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%" 
 
    fragments:  ~ 
 
    http_method_override: true 
 
    assets: ~ 
 

 
# Twig Configuration 
 
twig: 
 
    debug:   "%kernel.debug%" 
 
    strict_variables: "%kernel.debug%" 
 

 
# Doctrine Configuration 
 
doctrine: 
 
    dbal: 
 
     driver: pdo_mysql 
 
     host:  "%database_host%" 
 
     port:  "%database_port%" 
 
     dbname: "%database_name%" 
 
     user:  "%database_user%" 
 
     password: "%database_password%" 
 
     charset: UTF8 
 
     # if using pdo_sqlite as your database driver: 
 
     # 1. add the path in parameters.yml 
 
     #  e.g. database_path: "%kernel.root_dir%/data/data.db3" 
 
     # 2. Uncomment database_path in parameters.yml.dist 
 
     # 3. Uncomment next line: 
 
     #  path:  "%database_path%" 
 

 
    orm: 
 
     auto_generate_proxy_classes: "%kernel.debug%" 
 
     naming_strategy: doctrine.orm.naming_strategy.underscore 
 
     auto_mapping: true 
 

 
# Swiftmailer Configuration 
 
swiftmailer: 
 
    transport: "%mailer_transport%" 
 
    host:  "%mailer_host%" 
 
    username: "%mailer_user%" 
 
    password: "%mailer_password%" 
 
    spool:  { type: memory }

DefaultController.php

<?php 
 

 
namespace AppBundle\Controller; 
 

 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
 
use Symfony\Component\HttpFoundation\Request; 
 

 
use AppBundle\Entity\Page; 
 

 
class DefaultController extends Controller 
 
{ 
 
    /** 
 
    * @Route("/", name="homepage") 
 
    */ 
 
    public function indexAction(Request $request) 
 
    { 
 
     // replace this example code with whatever you need 
 
     return $this->render('default/index.html.twig', [ 
 
      'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR, 
 
     ]); 
 
    } 
 

 

 
}

這工作,但它不是性感

class DefaultController extends Controller 
 
{ 
 
    /** 
 
    * @Route("/", name="homepage") 
 
    * @Route("/{_locale}/", name="homepage_locale") 
 
    */ 
 
    public function indexAction(Request $request) 
 
    { 
 
     // replace this example code with whatever you need 
 
     return $this->render('default/index.html.twig', [ 
 
      'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR, 
 
     ]); 
 
    }

+0

添加你想要什麼,目前還不清楚 – Blag

回答

1

JMSI18nRoutingBundle

爲了達到你想要什麼,我用了一次JMSI18nRoutingBundle。它允許您爲每個語言環境翻譯您的路線,並且還可以在URL中管理您的前綴。 YAML配置

例子:

jms_i18n_routing: 
    default_locale: en 
    locales: [de, en] 
    strategy: prefix_except_default 

如果你的URL是這樣的:

- /de/contact # Locale: de 
- /contact # Locale: en 

或者,如果您定義轉換爲路徑(這兩個URL仍指向同一個位置!):

- /de/kontakt # Locale: de 
- /contact # Locale: en 

There有關如何配置此麪包的詳細信息你怎麼能延長它的。

Symfony的方式

routing.yml文件,你應該定義路線:

app.locale: 
    resource: "@AppBundle/Controller/" 
    type: annotation 
    prefix: /{_locale} 
app: 
    resource: "@AppBundle/Controller/" 
    type: annotation 

然後Symfony的第一個找的是區域路線。請注意,從api.locale開始的路線首先是匹配模式,然後當您有路線名爲example.com/go(如GO存儲但縮短)時,Symfony會嘗試查找區域go。爲了防止這種情況,你可以使用requirements參數:

app.locale: 
    resource: "@AppBundle/Controller/" 
    type: annotation 
    prefix: /{_locale} 
    requirements: 
     _locale: en|de 

這是不幹燥的方式,你需要定義任何途徑兩次(有和沒有語言環境)。

在配置路由中使用defaults不會有幫助,因爲如果需要,Symfony路由會添加任何參數。隨着defaults路徑example.com應該工作,但如果你去example.com/about,Symfony的增加參數_locale到URI和結果將是example.com/en/about

[編輯]重要的事情還是Symfony的的路由是如何工作的。我們首先路線app.locale前綴/{_locale}和第二homepage與路徑/。總結路徑將是example.com/{_locale}/,而不是example.com/{_locale}。尾部的斜線在構建URI時非常重要。

沒有這個,路徑example.com/de正在尋找app的路由,它找不到和de類似的路由。

有你有可能是斜線的解決方案:Redirect URLs with a Trailing Slash