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

(09-08-2022, 09:08 AM)demyr Wrote:
(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

Not in this case Big Grin
Reply
#12

(This post was last modified: 09-08-2022, 02:47 PM by kenjis.)

Code:
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -37,6 +37,15 @@ $routes->set404Override();
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

+$routes->group('', ['namespace' => '\App\Controllers\Backend'], static function ($routes) {
+    $routes->get('backend/dashboard', 'Dashboard::index', [
+        'as' => 'dashboard.index'
+    ]);
+});
+$routes->group('', ['namespace' => '\App\Controllers\Frontend'], static function ($routes) {
+    $routes->get('/{locale}/([a-zA-Z0-9_-]+)/', 'Router::show/$1');
+});
+
/*
  * --------------------------------------------------------------------
  * Additional Routing

Code:
$ php spark routes

CodeIgniter v4.2.6 Command Line Tool - Server Time: 2022-09-08 16:36:45 UTC-05:00

+--------+---------------------------+-------------------------------------------+----------------+---------------+
| Method | Route                    | Handler                                  | Before Filters | After Filters |
+--------+---------------------------+-------------------------------------------+----------------+---------------+
| GET    | /                        | \App\Controllers\Home::index              |                | toolbar      |
| GET    | backend/dashboard        | \App\Controllers\Backend\Dashboard::index |                | toolbar      |
| GET    | {locale}/([a-zA-Z0-9_-]+) | \App\Controllers\Frontend\Router::show/$1 | <unknown>      | <unknown>    |
| CLI    | ci(.*)                    | \CodeIgniter\CLI\CommandRunner::index/$1  |                |              |
+--------+---------------------------+-------------------------------------------+----------------+---------------+

Or

Code:
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -26,6 +26,7 @@ $routes->set404Override();
// If you don't want to define all routes, please use the Auto Routing (Improved).
// Set `$autoRoutesImproved` to true in `app/Config/Feature.php` and set the following to true.
// $routes->setAutoRoute(false);
+$routes->setPrioritize();

/*
  * --------------------------------------------------------------------
@@ -37,6 +38,15 @@ $routes->set404Override();
// route since we don't have to scan directories.
$routes->get('/', 'Home::index');

+$routes->group('', ['namespace' => '\App\Controllers\Frontend', 'priority' => 1], static function ($routes) {
+    $routes->get('/{locale}/([a-zA-Z0-9_-]+)/', 'Router::show/$1');
+});
+$routes->group('', ['namespace' => '\App\Controllers\Backend'], static function ($routes) {
+    $routes->get('backend/dashboard', 'Dashboard::index', [
+        'as' => 'dashboard.index',
+    ]);
+});
+
/*
  * --------------------------------------------------------------------
  * Additional Routing
Reply
#13

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

Yes @demyr this issue was faced me the same and I used routes priority but were some also minor issues you know that at @includebeer
Code:
https://includebeer.com/en/blog/creating-a-multilingual-website-with-codeigniter-4-part-1
has also tutorial how to do this but still faced this issue at where I solved by using CI URL hepler
PHP Code:
if (url_is('admin*')) {
    
// ...

Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply




Theme © iAndrew 2016 - Forum software by © MyBB