• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing $indexPage in IncomingRequest for multi-app setup

Hi guys, I just bumped to something I need to have nice a way how to handle mutiple applications using single CI core.

My setup:

- /apps
   - /app1
    - /Config
  - /app2
    - /Config
- /common
   - /Config
- /system
- /public
  - index.php (defualt front controller)
  - app2.php (second app front controller)
  - .htaccess

I would like to use URL like "http://localhost/app2" and it should be directed to app2.php to properly run application and initialize app2.

For this purpose I use mod_rewrite:
    # Convert first segment to front controller if exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^([^.*\/]*).*$
    RewriteCond %{DOCUMENT_ROOT}/%1 -f
    RewriteRule ^([^.*\/]*)(\/.*).*$ $1.php$2 [L,NC,QSA]
    RewriteRule ^([^.*\/]*)$ $1.php [L,NC,QSA]

After that I set indexPage for each app in this case $indexPage = 'index.php' and $indexPage = 'app2.php'

Now we need just add hook to IncomingRequest class to parseRequestURI method like this -> when checking for segments
// or when first segment is equal to indexPage
elseif (strpos(trim($uri,'/'), $this->config->indexPage) === 0)
   $uri = (string) substr(trim($uri,'/'), strlen($this->config->indexPage));

What do you think ? Smile


Could you create two folders in /public instead?

- /public/app
  - index.php
  - .htaccess
- /public/app2
  - index.php
  - .htaccess

In each index.php, you would change the line for the app/Config/Paths.php file.

require realpath(FCPATH . '../apps/app1/Config/Paths.php') ?: FCPATH . '../apps/app1/Config/Paths.php';

require realpath(FCPATH . '../apps/app2/Config/Paths.php') ?: FCPATH . '../apps/app2/Config/Paths.php';

Hi craig, Thank You for your reply Smile 

I believe there would be still the same issue, since you need to tell server where you want to go.

Eg. We would have http://localhost/app2 -> processed in mod_revriew it would make http://localhost/app2/index.php but REQUEST_URI will stay the same '/app2' and it would look for this controller inside /public/app2/index.php.

I could do the same just using everywhere patter -> http://localhost/app2.php/segment2/segent3 etc... and it would work (I have this setup already). For me, I want to remove extension from index file and I belie we should be able remove even first segment when it is set as indexPage. This will mask what server do with routing. Smile

P.S. sorry I just realized I made wrong file tree in the first post. I already corrected it.  And one more thing. My setup treats those apps as modules as well so they can be interconnected and using one common module with shared resources  Smile

I think it would be easier and cleaner to setup a subdomain for each app: app1.localhost, app2.localhost.
Then you won’t have to mess with mod_rewrite rules and multiple front controllers with different names.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com

(01-31-2021, 09:35 AM)includebeer Wrote: I think it would be easier and cleaner to setup a subdomain for each app: app1.localhost, app2.localhost.
Then you won’t have to mess with mod_rewrite rules and multiple front controllers with different names.

Hi, it is not an option. The System is interconencted and can have xy applications. It works under one sysem. Imagine this setup like Operating System. You have core framework and common libraries to take care about permissions and core functions and then you have fully encapuslated components (apps) which are indipendent (if it is required) or interconnected with the rest.

For me it is not a problem like I mentioned, I have it already working and tested. I tought it could be nice feature for CI. Smile

This way of setup brings clear separation of apps, easy upgrading and less chance to break other compoenent since you always know if you use common component or dedicated comoponents for that app.  Easy and rapid development of new apps for setup and more... I resolved most of the problems and now it seems to be very efficient way.

Good if it works for you.

I also found this in the user guide for setting up multiple applications using the same system directory: http://codeigniter.com/user_guide/genera...stallation

But since they all have their own public directory I guess it would require a subdomain for each application. I’m not sure, l guess you could also make some magic with mod_rewrite rules and point them to an alias.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com


yes I am aware about this feature. It was my starting point. This setup is for running completely different application using just simply one framework core, so I guess less upgrading work.

My setup work as one system with mutiple applications, not just using single CI core. Plus this is not breaking change, it can be implemented without anything breaking current projects.

Simply approach is to have one entry point that splits to different applications under one system setup except mutiple entry points for indiviudal applications using single core. BTW we have already this setup suported. The only issue here is that you have to define first segments as .php file. My solution will simply mask this step under simple segment and user have no idea what is done on background.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.