Welcome Guest, Not a member yet? Register   Sign In
LOCALE IS BEING SET TO DEFAULT BECAUSE OF URI FILTER
#1

I have a multilanguage site which was working fine until I had to create a filter for checking permission from the routes page,
this is my permission filter:- 
PHP Code:
public function before(RequestInterface $request$arguments null)
    {  
        
if (session()->get('isProviderLoggedIn') || Session()->get('isLoggedIn')) {
            helper('permission');
            if (is_admin() || permissions($arguments[0])) { 
                return true;
            } else if(is_provider() || permissions($arguments[0])){
                return true;
            } else {
                if (Session()->get('isLoggedIn')) {
                    return redirect()->route('admin-dashboard');
                } else if(session()->get('isProviderLoggedIn')) {
                    return redirect()->route('provider-dashboard');
                }
            }
        } else {
            return redirect()->to(site_url('admin'));
        }
    
this is how I am using this filter
PHP Code:
    $routes->get('admin/settings/roles/permission''RolesController::permission', ['filter' => 'permission:role_permission_view']); 

if I remove this filter part from routes, multilanguage is working fine, but with this filter, the locale is being set to 'en' by default.
Learning Codeigniter 
Reply
#2

Then there is something in your filter that is either blocking or over writing the local from being set.

Maybe your redirect is by passing the local.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(09-24-2022, 12:59 AM)InsiteFX Wrote: Then there is something in your filter that is either blocking or over writing the local from being set.

Maybe your redirect is by passing the local.

I am setting the locale from a very separate controller there is no way permission filter can be a reason, though I am not sure about the insights. 

Controller where I am setting locale-:
PHP Code:
class Language extends BaseController
{
    public function index()
    {
        $locale strtolower(substr($this->request->getLocale(), 02));
        $locale in_array($locale$this->request->config->supportedLocales) ? $locale $this->request->config->defaultLocale;
        $session session();
        $session->remove('lang');
        $session->set('lang'$locale);
        return redirect()->back();
    }


and locale filter to set locale
PHP Code:
public function before(RequestInterface $request$arguments null)
    {
        if (session()->has('lang'))
        {
            service('language')->setLocale(session('lang'));
        } else {
            // Save locale to session
            session()->set('lang'service('request')->getLocale());
        }
    
Learning Codeigniter 
Reply
#4

Most users set it in the BaseController. Here is IncludeBeer's Site that shows how they did it.

Creating a multilingual website with CodeIgniter 4 (part 1)

Creating a multilingual website with CodeIgniter 4 (part 2)
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB