Welcome Guest, Not a member yet? Register   Sign In
{locale} placeholder in routes
#9

(This post was last modified: 06-11-2022, 05:26 AM by webdeveloper.)

(06-10-2022, 06:34 PM)includebeer Wrote:
(06-10-2022, 06:05 PM)kenjis Wrote: See this PR: https://github.com/codeigniter4/CodeIgniter4/pull/6073

I like the idea of a $useSupportedLocalesOnly option. No need to hack the default behaviour with a filter!

The main question is why you run into this problem, that you need to redirect with right locale? How does the wrong locale appear in your URL? Is it even possible to happen just so?

@kenjis This makes the solution which I posted even better! Like that.

DONE. Fallback behaviour implemented.

Add to: app/Config/Routes.php

PHP Code:
$routes->useDefinedLocalesOnly(true); 

Create file: app/Services/Routes.php

PHP Code:
<?php

namespace App\Services;

use 
CodeIgniter\Autoloader\FileLocator;
use 
CodeIgniter\Router\RouteCollection;
use 
Config\Modules;

class 
Routes extends RouteCollection
{
    /**
    * @var bool
    */
    public bool $useDefinedLocalesOnly false;

    public function __construct(FileLocator $locatorModules $moduleConfig) {
        parent::__construct($locator$moduleConfig);
    }

    /**
    * @param bool $value
    * @return $this
    */
    public function useDefinedLocalesOnly(bool $value): self {
        $this->useDefinedLocalesOnly $value;

        return $this;
    }


Add to: app/Services/Router.php (check the code in first post to find the place where to put this)

PHP Code:
                    /**
                    * CORE EDIT START
                    */
                    if ($this->collection->useDefinedLocalesOnly) {
                        $supportedLocales config('App')->supportedLocales;

                        if (!in_array($temp[$localeSegment], $supportedLocales)) {
                            return false;
                        }
                    }
                    /**
                    * CORE EDIT END
                    */ 

Add use to: app/Config/Services.php

PHP Code:
use App\Services\Router

and function

PHP Code:
    /**
    * @param bool $getShared
    * @return Routes
    */
    public static function routes($getShared true): Routes {
        if ($getShared) {
            return static::getSharedInstance('routes');
        }

        return new Routes(AppServices::locator(), config('Modules'));
    
Reply


Messages In This Thread
{locale} placeholder in routes - by webdeveloper - 06-08-2022, 06:23 AM
RE: {locale} placeholder in routes - by InsiteFX - 06-09-2022, 11:57 PM
RE: {locale} placeholder in routes - by kenjis - 06-10-2022, 06:05 PM
RE: {locale} placeholder in routes - by webdeveloper - 06-11-2022, 04:29 AM



Theme © iAndrew 2016 - Forum software by © MyBB