Welcome Guest, Not a member yet? Register   Sign In
Eventually CRACKED IT - Menu driven multiple CodeIgniter applications
#1

[eluser]John_Betong[/eluser]
Hi All,

Preamble

Delphi has a wonderful technique that allows the programmer to easily switch
between applications by using a menu system.

CodeIgniter's rigidity and hard-coding has been bothering me for quite some time
as to the easist way to achieve:
   1. easily switch CodeIgniter applications
   2. use a common codeIgniter System folder.

I used to manually edit my "index.php" file but now with a slight modification
to the file I can now select the application from "_menu.php".
 
 
Usage
I have the following application directories setup: each directory is copied
from CodeIgniter's system/application folder.

My Localhost directory structure
Code:
...
    ci_bandwidthmeter
    ci_betsprint
    ci_fred
    ci_jokes
    ci_plaroma
    ci_rentaroofbox
    ci_system // renamed CodeIgniter's "system" folder for consistency
    ...
    index.php // modified
    _menu.php //

 

The index.php was slightly modified so that instead of having
"$application_folder" hard-coded it now reads from an "_application.php" file.
I wrote a "_menu.php" file that generates the "_application.php" file.

index.php
Code:
...
  ...
  // old code that had to be manually edited  to select different applications
  // the required application was copied and pasted to be the last declaration
    $application_folder = 'ci_bandwidthmeter';
    $application_folder = 'ci_betsprint';
    $application_folder = 'ci_fred';
    $application_folder = 'ci_jokes';
    $application_folder = 'ci_plaroma';
    $application_folder = 'ci_rentaroofbox';
    
  // new _menu.php driven setting  
  include('_application.php'); // contents generated from "_menu.php"
  $application_folder = APPLICATION_FOLDER; // included contents from  "_application.php" file
  ...
  ...
 
_menu.php
Code:
<?php

  // file name to save CodeIgniter's $application_folder variable
  $filename = getcwd() .'/_application.php';  

  // delete file that holds the $application_folder variable if and only if it exists
  if (file_exists($filename)) {
    unlink($filename);
  }

  // check to see if a $application has been passed at the command line
  $application   = isset($_GET['application']) ? $_GET['application'] : ''; // '$application is NOT set' ;
  
  // write the $application variable to the $filename file
  if ($application) {
    $application   =  "<?php define('APPLICATION_FOLDER', $application) ?>";
    
    $saved  = file_put_contents($filename, $application);
    if ($saved) {
      header("Location: index.php"); /* Redirect browser */
      exit;
    }else{
      // echo 'Message goes here ==> '. $filename;
    }
  }//endif $application
    
  
  // optional to eliminate html errors
  if (file_exists('_head.php')) {
    include('_head.php');
  }else{
    echo '<html><title>My CodeIgiter menu</title></head>';
  }
    
  // fall through to menu selections using radio buttons
?>

<body style='background:#f00 none; color:#00f'>

  <div style='width:40%; margin:4em auto; background:#fff none; color:#000; border:outset 6px; padding:1em'>

    &lt;form action='_menu.php' style='font-size:1.4em'&gt;
      <dl>
        <dt style='color:#f00'>My CodeIgniter Applications<br /><br /></dt>
           &lt;?php
              $b = array(
                          /* 'system/application', */
                          /* 'plaroma-web-vhost/40c074f1c862da07d2a392dfdd89dd23',*/
                          'ci_bandwidthmeter',
                          'ci_betsprint',
                          'ci_fred',
                          'ci_jokes',      
                          'ci_plaroma',
                          'ci_rentaroofbox'
                        );  
      
              $ddstart = "<dd>&lt;input type='radio' name='application' value='" ; // ."'  /&gt;" ;
              foreach($b as $row):
                echo $ddstart . $row ."'  />$row</dd>";
              endforeach;  
           ?&gt;  
         <dt>
            <br />
            &lt;input type="submit" value="&nbsp;&nbsp;Continue&nbsp;&nbsp;" class='button' /&gt;
            <br />
         </dt>
        
        </dl>
    &lt;/form&gt;
  </div>
&lt;/body&gt;
&lt;/html&gt;
&nbsp;
I am curious to know if anyone else has done something similar?
&nbsp;
Cheers,
&nbsp;
John_Betong
&nbsp;
&nbsp;
#2

[eluser]coolfactor[/eluser]
So,
1. You're using a form to select the application
2. When submitted, it creates/replaces an _application.php file that defines an APPLICATION_FOLDER constant.
3. index.php reads _application.php and launches the application defined in APPLICATION_FOLDER.

Am I right?

Question - how persistent does the application need to be? If it's only session-based, I'd probably opt to store the Application name in a standard PHP Session variable rather than a file. Just from a performance standpoint, checking for the existence of a file takes way longer than checking for a session variable. Second, the directory where the file was stored would need write privileges, reducing the overall security of the setup.
#3

[eluser]John_Betong[/eluser]
Hi CoolFactor,

Quote:1. You're using a form to select the application
2. When submitted, it creates/replaces an _application.php file that defines an APPLICATION_FOLDER constant.
3. index.php reads _application.php and launches the application defined in APPLICATION_FOLDER.

Am I right?
Yes you are completely correct in all three assumptions.
&nbsp;

Quote:Question - how persistent does the application need to be? If it's only session-
based, I'd probably opt to store the Application name in a standard PHP Session
variable rather than a file. Just from a performance standpoint, checking for
the existence of a file takes way longer than checking for a session variable.
Second, the directory where the file was stored would need write privileges,
reducing the overall security of the setup.
&nbsp;
Answer - The basic idea is only to make selecting different applications at the
Locahost platform. I agree about the session-based variable would be far better
especially from a performance standpoint..
&nbsp;
Unfortunately I have never completely come to terms with session_start(),
unset(), SID, etc. With error_level(E_STRICT) always produces warnings and/or
errors that I am unable to get round. I have just tried using sessions() and
tied myself in knots. I will give it a try later since I have work to doSmile
&nbsp;
Cheers,

John_Betong
&nbsp;
#4

[eluser]John_Betong[/eluser]
Hi CoolFactor,

I had time on my hands so revised the following to utilise $_SESSION variables as you suggested in your previous post:
&nbsp;
Code:
&lt;?php
  error_reporting(E_STRICT);
  
  // session_unset(); // reset session regardless and start again later ???
  session_start();
  if (isset($_GET['application']) ) {
    session_start();      
      $_SESSION['_MENU_'] = $_GET['application'];
      header("Location: index.php");  // Redirect browser
      exit;
    }else{
      // echo 'Yes we have no $_GET variable passed at the command line';
      // die;
    }
    
       // optional to eliminate html errors
       if (file_exists('_head.php')) {
         include('_head.php');
       }else{  
        echo '&lt;html&gt;&lt;title&gt;My CodeIgiter menu&lt;/title&gt;&lt;/head&gt;';
       }
?&gt;

The above code precedes my LOCALHOST menu system which you can see here ONLINE

I had to change my index.php setting:
Code:
$application_folder = isset($_SESSION['_MENU_']) ? $_SESSION['_MENU_'] : 'ci_jokes';

Cheers,

John_Betong
&nbsp;
&nbsp;
#5

[eluser]jstine[/eluser]
Seems like a nice solution. I got close to implementing it, however unfortunately on a remote server the $_SESSION solution doesn't seem to work. For some reason the index.php file is not able to set the $application_folder to a session variable, even though the variable is in fact set.
#6

[eluser]John_Betong_002[/eluser]
I know it is no consolation but it works fine for me Smile

Here is a checklist:
1. are your different application folders OK
2. do you have an existing working application?
3. have you tried renaming your application folder
4. have you set session_start(); in your index.php?

Can you try this code in your index.php and post the results?
Code:
...
  ...
  $application_folder = isset($_SESSION['_MENU_']) ? $_SESSION['_MENU_'] : 'default_application';
  if(1) // toggle to 0 to ignore
  {
    echo '<br />$application_folder: ', $application_folder;
    echo '<pre>';
      print_r($_SESSION);
    echo '</pre>';
    die;
  }
  ...
  ...
&nbsp;
&nbsp;
&nbsp;
#7

[eluser]jstine[/eluser]
Hi. Thanks for the reply.

So I added the code to my index.php and the result was

$application_folder: default_application

No session data is returned. It's as if the index.php file is just ignoring the Session information.

The session doesn't seem to be started in the index.php file

Interestingly, if I echo the $_SESSION['_MENU_'] value in the main (my default) controllers index function it displays the correct application name, it just seems that the index.php file wants to ignore the $_SESSION variable.

I would love to figure this out so if you have any more suggestions that would be great.
#8

[eluser]jstine[/eluser]
Oh, and I forgot to mention - all my applications load properly is I just change the $application_folder in the index.php menu manually.
#9

[eluser]John_Betong_002[/eluser]
[quote author="jstine" date="1300243811"]Oh, and I forgot to mention - all my applications load properly is I just change the $application_folder in the index.php menu manually.[/quote]

Try this link:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Menu to set CI application paths

Instructions for usage:
Code:
1. copy highlighted source code from above link.
2. paste into a new sub-directory/index.php.
3. edit your ROOT index.php file:
   // $application_folder = 'application';
   $application_folder = isset($_SESSION['_MENU_']) ? $_SESSION['_MENU_'] : "application";

4. Test and report any errors or undocumented features
&nbsp;
&nbsp;
&nbsp;
#10

[eluser]jstine[/eluser]
Thanks for trying to get this to work for me. Unfortunately it's still not working. It's very strange, if I uncomment the code in your new index.php

Code:
print_r($_GET);
    echo "<br />";
    print_r($_SESSION['_MENU_']);die;

I get the all the right values. However using the previous code you gave me below in my root index.php still just returns the equivalent of "$application_folder: application" in all instances and nothing for the $_SESSION (it's as if the $_SESSION doesn't exist)

Code:
if(1) // toggle to 0 to ignore
  {
    echo '<br />$application_folder: ', $application_folder;
    echo '<pre>';
      print_r($_SESSION);
    echo '</pre>';
    die;
  }

It's very strange as the new index.php file in my sub directory returns the right value for $_SESSION['_MENU_'] and if I echo $_SESSION['_MENU_'] in my default application file (main/index function) it returns the correct value as well. It's as if the root index.php file is ignoring the $_SESSION. I'm wondering if it's a server setting? Or another setting in my Codeigniter configuration?

Again, thanks for the help.




Theme © iAndrew 2016 - Forum software by © MyBB