CodeIgniter Forums
URL routing for localized frontend and backend - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: URL routing for localized frontend and backend (/showthread.php?tid=82997)

Pages: 1 2


URL routing for localized frontend and backend - janroz - 09-08-2022

Hello,

I would like to ask if anyone has solved a similar problem. I have gone through the discussion forum but no advice has helped me.

I have the following problem. We have a website where the frontend is localized:

domain.com/en/contact
domain.com/de/kontakt
domain.com/it/contatto

This is fine, it works fine for me. I set it up this way:

PHP Code:
$routes->group('', ['namespace' => '\App\Controllers\Frontend'], static function ($routes) {
    $routes->get('/{locale}/([a-zA-Z0-9_-]+)/''Router::show/$1');
}); 

We have the controllers divided like this:

/app/Controllers/Backend/
/app/Controllers/Frontend/

When I added {locale} to the URL, my backend stopped working. I can't figure it out, can someone help me?

PHP Code:
$routes->group('', ['namespace' => '\App\Controllers\Backend'], static function ($routes) {
    
$routes->add('backend/dashboard''Dashboard::index', [
        
'as' => 'dashboard.index'
    
]);
}); 

The application returns a 404 error because there is no language code in the given URL for the backend.

I would need to introduce some kind of exception so that the above rules don't apply to /backend. So that the URLs are localized only when I'm not in the backend.

Can someone help me?


RE: URL routing for localized frontend and backend - kenjis - 09-08-2022

{locale} in the route definition matches any URI segment.


RE: URL routing for localized frontend and backend - janroz - 09-08-2022

(09-08-2022, 04:36 AM)kenjis Wrote: {locale} in the route definition matches any URI segment.

Thank for reply, I know about that, but it is not answer for my question.


RE: URL routing for localized frontend and backend - kenjis - 09-08-2022

See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#route-priority


RE: URL routing for localized frontend and backend - janroz - 09-08-2022

(09-08-2022, 05:00 AM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#route-priority

I tried, it did not solved my problem. I have temporary solution, I will post it here in a while.


RE: URL routing for localized frontend and backend - kenjis - 09-08-2022

By the way, you are using $routes->add(), but it has security risk, so I recommend not to use.
See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#routes-with-any-http-verbs


RE: URL routing for localized frontend and backend - luckmoshy - 09-08-2022

Your issue happened to me and was 100% like your issue I solved by this way I tell you 100% use this logic condition URL helper it will 100%  work


PHP Code:
<?php if (url_is('admin')) {
    // ...your routes



https://codeigniter.com/user_guide/helpers/url_helper.html?highlight=url#url_to


RE: URL routing for localized frontend and backend - demyr - 09-08-2022

After using {locale} it is normal (for me) to face with this problem (or maybe I'm used to it). You should also use {locale} for the backend part. That's the only solution for me.


RE: URL routing for localized frontend and backend - janroz - 09-08-2022

(09-08-2022, 07:54 AM)demyr Wrote: After using {locale} it is normal (for me) to face with this problem (or maybe I'm used to it). You should also use {locale} for the backend part. That's the only solution for me.

Temporary solved like this (I am sure there is better solution):

PHP Code:
if ($request->uri->getSegment(1) !== 'backend') {
    
    
$routes->get('/''\App\Controllers\Frontend\Router::homepage');

    
$routes->get('/{locale}''\App\Controllers\Frontend\Router::homepage', [
        
'as' => 'homepage']
    );

    
// ... routes continues for frontend


Then continues rules for backend - without locale. It makes no sense to have locale in backend URL.

kenjis Wrote:By the way, you are using $routes->add(), but it has security risk, so I recommend not to use.
See https://codeigniter4.github.io/CodeIgnit...http-verbs

Thank you, I updated it in my config.


RE: URL routing for localized frontend and backend - demyr - 09-08-2022

(09-08-2022, 08:56 AM)janroz Wrote:
Then continues rules for backend - without locale. It makes no sense to have locale in backend URL.

Maybe you will have admin users from other nations ? Big Grin