CodeIgniter Forums

Full Version: Problem in routing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.
Created a folder "Modules" with modules in the root ...
In file autoload.php
PHP Code:
$psr4 = [
            
'Config'      => APPPATH  'Config',
            
APP_NAMESPACE => APPPATH,                // For custom namespace
            
'App'         => APPPATH,                // To ensure filters, etc still found,
            
'Modules'     => ROOTPATH 'Modules',
        ]; 


structure in the module folder:
Content
   - Config
   - Controllers
   - Models
   - Libraries
   ...
   ...
create controller: ContentAdminController
PHP Code:
namespace Modules\Content\Controllers;

// use CodeIgniter\Controller;

class ContentAdminController extends \CodeIgniter\Controller
{
    public function index()
    {
    echo 
123;
    }


add route:
PHP Code:
$routes->add('admin''ContentAdminController::index', ['namespace' => 'Modules\Content\Controllers']); 
url is work!
http://name_site.ru/admin

add route:
PHP Code:
$routes->add('admin/(:any)''$1::index', ['namespace' => 'Modules\Content\Controllers']); 
url is not work!
http://name_site.ru/admin/ContentAdminController

Help, why the class does not work dynamically? thanks
Why, no one will help?
It must be:
PHP Code:
(required*) controller class specified :: (optionalcontroller method \ (for different queries) $

you can't use $n as a method placeholder or controller class itself.
in General it's something like requests made after the sign "?" in the address bar.

Example:
PHP Code:
https://example.com/controller/method/?other-query 

-> ?other-query it is substituted in a sign "$"
and that your site understood that to catch in requests and that to pass by, it is necessary to use "placeholders".


In Routes.php

PHP Code:
$routes->add('admin/(:any)', 'ContentAdminController::index/$1'); 

In ContentAdminController.php

PHP Code:
class ContentAdminController extends Controller
{
         public function 
index(string $any) {
             echo `
Any segment: {$any}`;
         }

PHP Code:
$routes->add('admin/([a-z0-9+])/(:any)''$1::index/$2'); 
$1 - Controller
$2 - Method

So is it impossible?
there is no, so no one makes, I higher already led example the right use...

"$1" is essentially similar to get requests, that is, $_GET,
but usually this placeholder is used together with models and database,
depending on what gets into this placeholder, it will be derived from the database.
You also need to namespace the Content folder.

PHP Code:
'Modules\Content' => ROOTPATH 'Modules\Content'