Welcome Guest, Not a member yet? Register   Sign In
How can you configure routes for such a structure.
#1

(This post was last modified: 11-02-2018, 01:38 AM by Dedov_Evgeniy.)

Hello, help me please.
How can you configure routes for such a structure.

Application
Modules
--Shop
----Controllers
------Admin
--------ControllerAdmin_1.php
--------ControllerAdmin_2.php

------ControllerPublic_1.php
------ControllerPublic_2.php

----Models
----Views
----...
----...

--Blog
--Forum
...
...

Need to...

http://site.com/admin/blog/....
to
Modules/Blog/Controllers/Admin/ControllersAdmin.php

And
http://site.com/blog/....
to
Modules/Blog/Controllers/ControllersPublic.php

So you can't?
PHP Code:
$routes->get('admin/([0-9a-z_-]+)''Modules\$1\Controllers\Admin\Index::index'); 


Thank.
Reply
#2

First off you need to add the Modules an all the other directories to the PSR4 
in  ./application/autoload.php

Something like this, just an Example.

PHP Code:
$psr4 = [
        
'Config'      => APPPATH 'Config',
        
APP_NAMESPACE => APPPATH                   // For custom namespace
        
'App'         => APPPATH                   // To ensure filters, etc still found,
        
'Modules'     => ROOTPATH.'Modules',        // Modules Location
        
'Shop'          => ROOTPATH.'Modules/Shop',    // Shop location
        
'Blog'          => ROOTPATH.'Modules/Blog',    // Blog Location
        
'Forum'          => ROOTPATH.'Modules/Forum',    // Forum Location
        
]; 

In your routes file use the namespace where your modules etc are.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Can I use my route file in the module itself? The documentation seems to say, but I can not understand how this can be done ...
https://bcit-ci.github.io/CodeIgniter4/g...dules.html
Reply
#4

Yes you can, but make sure you setup your namspace's in the PSR4
For routes in a module make sure you add the namespace to the root.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

As InsiteFX said, make sure your Modules are setup like he showed previously (with each module being listed). Then create a new routes file at Modules\Blog\Config\Routes.php. Those routes should be automatically read by the default configuration.

To answer the original question - the problem you're running into is the difference in capitalization with the uri having a lowercase blog and the namespace being ucfirst Blog so it's not finding the class.
Reply
#6

Tell me please...
Is it possible to achieve a universal solution in CI4?
For example, we have the HMVC extension installed in CI3.

The routes are configured so that when installing the module, we just need to upload the folder with the module to the server, and that's it ... There is no need to edit anything else, the main file of the routes remains as it is. All routes in your module file (routes.php).
In CI4 it turns out, you need to make an entry in the PSR4 + entry in the route file, which is not very convenient, especially for our clients.
Please advise whether it is possible to achieve such simplicity as in CI3. Thank.
Reply
#7

That is not currently possible. At this point we have a feature freeze until CI4 is released, but I imagine you could extend the system in some way to automatically discover name-spaces without too much hassle.
Reply
#8

I would be very grateful if you show an example with the code how this can be done, Thank you.
Reply
#9

There is such an idea ... I would like to ask if she has the right to life, or it will turn out completely silly.
PHP Code:
$routes->group('admin/([0-9a-z_-]+)', function($routes)
{
    
$uri = \Config\Services::request()->uri;

    
$module $uri->getSegment(2);

 
   $routes->add('/''Modules\\'.$module.'\\Controllers\Admin\Index::index');
}); 
Reply
#10

I haven't tried anything like that so I can't provide example code or say definitely what will work and what won't. Just try it and see what happens!

I will say, though, that in the example you just gave, you need to ensure the case of $module must match the case of your namespace, which is likely ucfirst. So, making this might work:

Code:
$module = ucfirst($uri->getSegment(2));
Reply




Theme © iAndrew 2016 - Forum software by © MyBB