Welcome Guest, Not a member yet? Register   Sign In
URL routing for localized frontend and backend
#1

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?
Reply
#2

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

(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.
Reply
#4

See https://codeigniter4.github.io/CodeIgnit...e-priority
Reply
#5

(09-08-2022, 05:00 AM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgnit...e-priority

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

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
Reply
#7

(This post was last modified: 09-08-2022, 07:54 AM by luckmoshy.)

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/helpe...url#url_to
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#8

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.
Reply
#9

(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.
Reply
#10

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB