Cakephp 3 url参数默认值

时间:2016-03-31 21:47:38

标签: php cakephp routing cakephp-3.0

我想在我的网站中添加语言参数链接。

类似的东西:

http://www.example.com/en/home

这是我的routes.php文件:

use Cake\Core\Plugin;
use Cake\Routing\Router;
use Cake\ORM\TableRegistry;
use Cake\Core\Configure;

Router::defaultRouteClass('Route');

Router::addUrlFilter(function ($params, $request) {
    if (isset($request->params['language']) && !isset($params['language'])) {
        $params['language'] = $request->params['language'];
    }
    else{
        $params['language'] = 'en';
    }
    return $params;
});

Router::scope('/', function ($routes) {
    $lang = 'en|it|fr';

    $routes->connect('/', ['language' => 'en', 'controller' => 'Pages', 'action' => 'index', 'index']);

    $routes->connect(
        '/:language/:controller',
        ['action' => 'index'],
        ['language' => $lang]
    );

    $routes->connect(
        '/:language/:controller/:action',
        ['language' => $lang]
    );

    $routes->fallbacks('InflectedRoute');
});

Plugin::routes();

问题是:

  • 当我写这个网址时:http://example.com/admins它正确地成为http://example.com/en/admins/index

  • 当我写这个网址时:http://example.com/admins/index它变为http://example.com/admins/index 不正确

我该如何解决?

0 个答案:

没有答案