doufen1890 2016-08-19 11:53
浏览 53
已采纳

Silex 2:在路由中声明{_locale}可选

I would like routing these kind of URLs:

/hello/{name}
/en/hello/{name}
/es/hello/{name}

I have created this Controller class:

<?php

namespace Fw\Controllers;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Fw\Application as FwApplication;

class Hello implements ControllerProviderInterface {

    const URL_BASE = '/hello';
    const URL_BASE_LOCALE = '/{_locale}/hello';

    public function connect(Application $app) {
        $controllers = $app['controllers_factory'];

        $controllers
                ->get('/{name}/', array($this, 'nameAction'))
                ->bind('hello_name')
        ;

        return $controllers;
    }

    public function nameAction(FwApplication\ApplicationBase $app, Request $request, $name) {
        return new Response('Hello ' . $name, 200);
    }
}

And it has been registered in this way:

$app->mount(FwControllers\Hello::URL_BASE_LOCALE, new FwControllers\Hello());
$app->mount(FwControllers\Hello::URL_BASE, new FwControllers\Hello());

This URL works good: /hello/foo But this one not: /en/hello/foo, this error is shown:

NotFoundHttpException in RouterListener.php line 125: No route found for "GET /en/hello/foo/"

It seems that the second "mount" sentence is overwritten to first one.

Can anybody help me with this problem? How can I set routing with optional {_locale}?

Thanks.

  • 写回答

1条回答 默认 最新

  • drtoaamk20278 2016-08-19 13:58
    关注

    I had same problem and wrote service provider that automatically adds locale to all routes https://github.com/pmaxs/silex-locale. May be it will satisfy your requirements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀