CodeIgniter + Ion Auth: Ion Auth config file is loaded twice - php

Explanation:
Using CodeIgniter v3.1.2 with Ion Auth 2
I separated the admin area and user area into 2 different applications to improve security.
I wanted to share config files between two apps so for application_admin/config/ion_auth.php I did this:
require_once(__DIR__.'/../../application_users/config/ion_auth.php');
The script stopped working and after some hours I realized the file is being loaded twice because when I use require instead of require_once everything work's fine.
Questions:
Shouldn't CodeIgniter check and prevent loading files that are already loaded twice? if yes then why is this happening, and if not is it okay if I just use require instead of require_once?
Is my approach the correct way of sharing config files and helpers between multiple applications in a single CI install?
Edit:
I put the require_once in the admin/config/ion_auth.php to include user/config/ion_auth.php so basically I'm putting the content of that config file in this config file "only when CodeIgniter itself loads this config file".
Also even if I put the normal ion_auth config file without any require/include etc, it is still loaded twice (I noticed by putting a simple echo 'loaded<br>'; in the end of config file.)

Shouldn't CodeIgniter check and prevent loading files that are already
loaded twice? if yes then why is this happening, and if not is it okay
By making direct call to require or require_once you are bypassing Codeigniter (CI) so it has no way of knowing what you have or have not included.
if I just use require instead of require_once? Is my approach the
correct way of sharing config files and helpers between multiple
applications in a single CI install?
Loading a config file in CI should be done using the Config class. If you dig into the source code for Ion Auth and look at __construct in Ion-auth_model.php you will find heavy use of the config class to read the config file and access various config items. It a good example of the "CI way" to handle configuration.

Related

Is there no manual include of Laravel?

I was starting some .php files in the /public directory of Laravel, which works, naturally, but it is separated from the standard Laravel system. In fact, Laravel wants you to you routes I know, and if I want to use some Laravel stuff I would think that calling
require_once 'path/to/vendor/autoload.php';
require_once 'path/to/app/Services/Myfile.php';
would let one use that... but for example "use GuzzleHttp", if used in myfile, gets fatal "Uncaught ReflectionException: Class config does not exist" in Container.php of Laravel.
I know there's SHORTINIT in Wordpress though it is kind of a mess of requiring any file that you need and all the files with functions it uses... Is there something similar for Laravel? Or this is never properly used this way to hold php files within /public?
You don't need to use require as composer takes care of loading the library files.
However when you add any new library or make any changes to routes or config files make sure you run the optimize command.
php artisan optimize

Including PHP files with Laravel framework

I am trying to "include" a php script into one of my views (landing.blade.php).
The script is in:
/laravel-master/public/assets/scripts/config.php
When I try to include this code in the view:
<?php include_once('/assets/scripts/config.php'); ?>
I get the error: include_once(/assets/scripts/config.php): failed to open stream: No such file or directory
This is on localhost using MAMP. I'm not sure if there is a different set of rules I need to use with Laravel 4 to include a php file. Thank you for your help!
First, it's not really recommended that you keep your PHP files in the public directory, they should be kept inside the app folder. I'd suggest you create a folder inside app, something like includes and put your files there. Then, you include it, do:
include(app_path().'/includes/config.php');
Although, since it looks like you're trying to load some configuration files, I'd recommend you also check out Laravel's own way of handling configurations. For instance, if you created a myapp.php file inside the app/config folder, Laravel would handle it automatically for you, as long as you'd have some key-value pairs, like this:
<?php
return [
'name' => 'Raphael',
'gorgeous' => true
];
You could then retrieve these values using the Config class:
Config::get('myapp.name'); // Raphael
This is a better solution because you can also take advantage of Laravel's environment configuration.
You can use includes in HTML forget about concatenation
#include('foldername.filename')
#include('filename')
This is another way:
require 'file path';

CodeIgniter - extend native library in multiple-site setup

I want to use a central CI setup for multiple sites. The way I handle this is I created a package called MPACK and added it to autoload in the config file of each site.
Folder Structure:
/main
/system (CI 2 System folder)
/MPACK
/site1
/application
site2
/application
Inside this MPACK I have share libraries, models, helpers, etc.
However, I would like to have an extended MY_Form_Validation that would be common to ALL sites. Adding the class file to /MPACK/libraries fails. Adding it to /site1/application works fine, as expected.
Is there any way to do this extending inside MPACK?
Thank you for your time.
Please try this:
// Placed your MY_Form_validation.php under MPACK/libraries
$this->load->add_package_path('/path/to/MPACK');
$this->load->library('form_validation');
You can get more information from CodeIgniter User Guide - Loader Class. :)
You can also autoload your package in /application/config/autoload.php : $autoload['packages'] = array('/path/to/MPACK');
EDIT: turn out that the above solution doesn't work, because Loader always look for APPPATH & BASEPATH first, and I not sure modifying this core class won't break something. Here is another solution in theory:
You should have your MPACK form validation lib, and sites' form validation lib should be symlinks to the MPACK one:
/site1/application/MY_Form_validation.php -> /MPACK/libraries/MY_Form_validation.php
If you just use everything from MPACK, nothing specifically for /site1 or /site2, just make a folder link:
/site1/application/libraries/ -> /MPACK/application/libraries/
Hope this help =)
You can read more here: http://codeigniter.com/wiki/Multiple_Applications_via_Symlinks/

codeigniter direct file access

I've got a clean install of codeigniter running. I need to access a library file diretly, but i get Access forbidden all the time. Is there any way to fix this? can't find anything about this in the config files etc.
you should try using the codeigniter loader class, if i'm understanding the full question here, example below:
$this->load->library('email');

HMVC and Views in folders (Codeigniter)

I am using Tank Auth library in Codeigniter with HMVC and the entire tank auth mvc files are in its own module called 'auth'. tank auth loads a view (domain.com/application/modules/auth/views/auth/login_form.php) found inside a folder (auth) using:
$this->load->view('auth/login_form', $data);
As far as I know the above code will load login_form.php inside the auth folder properly without HMVC. However with HMVC, I need the following code to get the view to load:
$this->load->view('auth/auth/login_form', $data);
Is there a setting that we should change so we dont have to refer to the view file by (module name)/(views folder name)/(view filename) ? Or is this perfectly normal and most people does it this way?
It seems troublesome that I have to add the module folder name 'auth' to every view() function call, and change all of them should I change the name of the module folder.
Assuming you're using Modular Extensions - HMVC:
If you have auth set up as a module already, you can just call:
$this->load->view('login_form', $data);
The file /views/login_form.php will be loaded from within the current module. This applies to models, language files, libraries etc. Think of the module as its own application, this is what you would normally do.
Additionally, to load a file from another module or a controller outside the module's directory, you can use $this->load->view('auth/login_form');
If the file is not found, it will check the other module paths including the default directory. This may or may not be the way other HMVC packages work, I'm not sure - but it's the way MX works.

Categories