Welcome Guest, Not a member yet? Register   Sign In
Comparing $indexPage in IncomingRequest for multi-app setup
#1

(This post was last modified: 01-26-2021, 12:14 PM by xbotkaj.)

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:

Code:
- /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:
Code:
    # 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
Code:
// 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

THX
Reply
#2

Could you create two folders in /public instead?

Code:
- /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.

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

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

(This post was last modified: 01-26-2021, 12:14 PM by xbotkaj.)

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
Reply
#4

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
/*** NO support in private message - Use the forum! ***/
Reply
#5

(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.
Reply
#6

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
/*** NO support in private message - Use the forum! ***/
Reply
#7

(This post was last modified: 02-03-2021, 03:10 PM by xbotkaj.)

Hi,

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.
Reply
#8

(This post was last modified: 07-21-2021, 01:06 AM by pedritorres. Edit Reason: Spelling )

[quote pid="384112" dateline="1612390082"]
xbotkajHi,

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 applinked.me 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.

[/quote]


This is an amazing and informative thread man. Thank you


Reply




Theme © iAndrew 2016 - Forum software by © MyBB