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

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

The main goal is to be able define just one route instead multiple rows for every locale.

This
PHP Code:
    public $globals = [
        'before' => [
            'secured' => [
                'except' => [
                    '{locale}/auth/login',
                    '{locale}/auth/recovery'
                ]
            ]
        ]
    ];

    public $filters = [
        'is_logged_in' => [
            'before' => [
                '{locale}/auth/login',
                '{locale}/auth/recovery'
            ]
        ]
    ]; 

instead this
PHP Code:
    public $globals = [
        'before' => [
            'secured' => [
                'except' => [
                    'en/auth/login',
                    'en/auth/recovery',
                    'fr/auth/login',
                    'fr/auth/recovery',
                    'sk/auth/login',
                    'sk/auth/recovery',
                    ...
                ]
            ]
        ]
    ];

    public $filters = [
        'is_logged_in' => [
            'before' => [
                'en/auth/login',
                'en/auth/recovery',
                'fr/auth/login',
                'fr/auth/recovery',
                'sk/auth/login',
                'sk/auth/recovery',
                ...
            ]
        ]
    ]; 

Add use to app/Config/Services.php

PHP Code:
use App\Services\Filters

and function

PHP Code:
     /**
    * @param \Config\Filters|null $config
    * @param bool                $getShared
    * @return Filters
    */
    public static function filters(ConfigFilters $config null$getShared true): Filters {
        if ($getShared) {
            return static::getSharedInstance('filters'$config);
        }

        $config ??= config('Filters');

        return new Filters($configAppServices::request(), AppServices::response());
    

Create file app/Services/Filters.php 

We will change just function pathApplies() but because its private, we have to override all functions where is this function used

PHP Code:
<?php

namespace App\Services;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\Filters as CoreFilters;
use 
Config\Modules;

class 
Filters extends CoreFilters
{
    public function __construct($configRequestInterface $requestResponseInterface $response, ?Modules $modules null) {
        parent::__construct($config$request$response$modules);
    }

    /**
    * @param string|null $uri
    */
    protected function processGlobals(?string $uri null) {
        if (!isset($this->config->globals) || !is_array($this->config->globals)) {
            return;
        }

        $uri strtolower(trim($uri ?? '''/ '));

        // Add any global filters, unless they are excluded for this URI
        $sets = [
            'before',
            'after'
        ];

        foreach ($sets as $set) {
            if (isset($this->config->globals[$set])) {
                // look at each alias in the group
                foreach ($this->config->globals[$set] as $alias => $rules) {
                    $keep true;

                    if (is_array($rules)) {
                        // see if it should be excluded
                        if (isset($rules['except'])) {
                            // grab the exclusion rules
                            $check $rules['except'];

                            if ($this->pathApplies($uri$check)) {
                                $keep false;
                            }
                        }
                    } else {
                        $alias $rules// simple name of filter to apply
                    }

                    if ($keep) {
                        $this->filters[$set][] = $alias;
                    }
                }
            }
        }
    }

    /**
    * @param string|null $uri
    */
    protected function processFilters(?string $uri null) {
        if (!isset($this->config->filters) || !$this->config->filters) {
            return;
        }

        $uri strtolower(trim($uri'/ '));

        // Add any filters that apply to this URI
        foreach ($this->config->filters as $alias => $settings) {
            // Look for inclusion rules
            if (isset($settings['before'])) {
                $path $settings['before'];

                if ($this->pathApplies($uri$path)) {
                    $this->filters['before'][] = $alias;
                }
            }

            if (isset($settings['after'])) {
                $path $settings['after'];

                if ($this->pathApplies($uri$path)) {
                    $this->filters['after'][] = $alias;
                }
            }
        }
    }

    /**
    * @param string $uri
    * @param mixed  $paths
    * @return bool
    */
    private function pathApplies(string $uri$paths): bool {
        // empty path matches all
        if (empty($paths)) {
            return true;
        }

        // make sure the paths are iterable
        if (is_string($paths)) {
            $paths = [$paths];
        }

        // treat each paths as pseudo-regex
        foreach ($paths as $path) {
            // Reset localeSegment
            $localeSegment null;

            $path trim($path'/ ');

            // Are we dealing with a locale?
            if (strpos($path'{locale}') !== false) {
                $localeSegment array_search('{locale}'preg_split('/[\/]*((^[a-zA-Z0-9])|\(([^()]*)\))*[\/]+/m'$path), true);
            }

            // need to escape path separators
            $path str_replace('/''\/'$path);
            // need to make pseudo wildcard real
            $path strtolower(str_replace('*''.*'$path));

            if (isset($localeSegment)) {
                // Replace it with a regex so it
                // will actually match.
                $path str_replace('{locale}''[^\/]+'$path);
            }

            // Does this rule apply here?
            if (preg_match('#^' $path '$#'$uri$match) === 1) {
                if (isset($localeSegment)) {
                    $temp = (explode('/'$uri));

                    $supportedLocales config('App')->supportedLocales;

                    if (in_array($temp[$localeSegment], $supportedLocales)) {
                        return true;
                    }
                } else {
                    return true;
                }
            }
        }

        return false;
    }

Reply
#2

(This post was last modified: 06-12-2022, 03:15 AM by luckmoshy.)

@datamweb Where are these Class!!! ???
PHP Code:
return new Filters($configAppServices::request(), AppServices::response()); 
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply




Theme © iAndrew 2016 - Forum software by © MyBB