CodeIgniter Forums
Problem in routing - 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: Problem in routing (/showthread.php?tid=75072)



Problem in routing - Dedov_Evgeniy - 12-16-2019

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


RE: Problem in routing - Dedov_Evgeniy - 12-23-2019

Why, no one will help?


RE: Problem in routing - Digital_Wolf - 12-23-2019

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}`;
         }




RE: Problem in routing - Dedov_Evgeniy - 12-23-2019

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

So is it impossible?


RE: Problem in routing - Digital_Wolf - 12-24-2019

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.


RE: Problem in routing - InsiteFX - 12-24-2019

You also need to namespace the Content folder.

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