【问题标题】:Codeigniter 4 - Locale if does not exist getting error 404Codeigniter 4 - 如果不存在区域设置,则出现错误 404
【发布时间】:2023-03-09 13:40:02
【问题描述】:

我正在尝试使用 codeigniter 4 {locale} 组。

$routes->group('{locale}', function ($routes){
    $routes->group('customer', function ($routes){
        $routes->match(['get','post'],'/', 'Customer::index');
    });
});

当我访问 en/customer/ 时一切正常,但如果我尝试在没有区域设置的情况下访问 customer/ 我得到 404 错误。

我的默认语言en我想做如果路由中没有语言,页面应该使用默认语言。

【问题讨论】:

标签: php codeigniter codeigniter-4


【解决方案1】:

app/config.php


    /**
     * --------------------------------------------------------------------------
     * Default Locale
     * --------------------------------------------------------------------------
     *
     * The Locale roughly represents the language and location that your visitor
     * is viewing the site from. It affects the language strings and other
     * strings (like currency markers, numbers, etc), that your program
     * should run under for this request.
     *
     * @var string
     */
    public $defaultLocale = 'en';

    /**
     * --------------------------------------------------------------------------
     * Negotiate Locale
     * --------------------------------------------------------------------------
     *
     * If true, the current Request object will automatically determine the
     * language to use based on the value of the Accept-Language header.
     *
     * If false, no automatic detection will be performed.
     *
     * @var bool
     */
    public $negotiateLocale = true;

    /**
     * --------------------------------------------------------------------------
     * Supported Locales
     * --------------------------------------------------------------------------
     *
     * If $negotiateLocale is true, this array lists the locales supported
     * by the application in descending order of priority. If no match is
     * found, the first locale will be used.
     *
     * @var string[]
     */
    public $supportedLocales = ['en','fa'];


然后使用标题来更改语言 Accept-Language set To en or fa

【讨论】:

  • app/config.php 和你写的完全一样。我在哪里可以在 Codeigniter 中设置 Accept-Language 还是应该手动设置?
  • 使用 post man first testing app postman.com 你在发送的请求中添加标题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 1970-01-01
相关资源
最近更新 更多