• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How I can show 404 if locale invalid?

#1
How I can show 404 if locale invalid?

My route:
$routes->add('{locale}', 'App\Controllers\Home::index');

But if I try open mysite.com/asdasdasd, or mysite.com/dad2da ... - I get HTTP code 200, I want get 404.

I must write crutch?

Then, if I write filter, how I detect what first segment used as locale, not controller?
Reply

#2
tbh i dont know about CI4 but i will save the locale on cookie and just load it Tongue
Reply

#3
(11-24-2019, 11:58 AM)XTAZ Wrote: How I can show 404 if locale invalid?

My route:
$routes->add('{locale}', 'App\Controllers\Home::index');

But if I try open mysite.com/asdasdasd, or mysite.com/dad2da ... - I get HTTP code 200, I want get 404.

I must write crutch?

Then, if I write filter, how I detect what first segment used as locale, not controller?
I write this code:

PHP Code:
<?php namespace App\Filters;

use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;

class 
Locale implements FilterInterface
{
    public function 
before(RequestInterface $request)
    {
        
$supported_locales config('Config\\App')->supportedLocales;
        
$uri_locale $request->uri->getSegment(1);
        
        if (
in_array($uri_locale$supported_locales))
        {
            
// For pages outside of routing (404 and etc)
            
$request->setLocale($uri_locale);
        }
        
        
$use_locale $request->getLocale();
        
        if (
$uri_locale !== $use_locale)
        {
            return 
redirect()->to("/{$use_locale}/".trim($_SERVER['REQUEST_URI'], '/'));
        }
    }

    
//--------------------------------------------------------------------

    
public function after(RequestInterface $requestResponseInterface $response)
    {
        
// Do something here
    
}

If user try open site.com/awdawd, we redirects him to site.com/{defaultLocale}/awdawd and he view 404

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.