Creating Config File for Custom Library in CodeIgniter - php

I've looked through all of the CI documentation, and done some Googling of it, but I still can't quite seem to figure out how to create a configuration file for a custom library in codeigniter. If somebody could even just point me in the direction of where in the docs I could find my answer it would be greatly appreciated.
I am creating a library in CI that makes use of several database columns that can vary in name between applications, so I would like the names to be stored in a custom config file. Then I would like to be able to load these values in the construct of the library.
So my two questions are:
1.) How do I name the config file, and how do I name variables within that file so they don't overwrite any other config vars?
2.) How do I get the values from within my library?

When i have questions like this i like to look at other projects that already do this. We utilized Tank_auth in almost all of our ci projects. This is a popular authentication library, which has its own custome config files
It just creates its own config file in application/config directory.
You could prefix your config items with your app name to ensure that they are unique
it then just loads it in the constructor:
$this->ci->load->config('tank_auth', TRUE);

If there is a config/libraryname.php file, it will be automatically loaded, just before library instanciation.
(so, beware of name conflicts with CI's config files)
Side note: this autoloading is disabled if you pass an array as the 2nd argument:
$this->load->library('thelibrary', array('param1' => 'value1'));

in your config /config/your_conf.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = Array(
'your_conf1' => 'your_conf_val1',
'your_conf2' => 'your_conf_val2',
'your_conf3' => 'your_conf_val3'
);
in your controller:
$this->config->load('your_conf');
var_dump((array)$this->config); //show all the configs including those in the your_conf.php

Related

Override JSON view in RequestHandler in CakePHP

I want to rewrite JSON View in the RequestHandler. So there's a file project_root/lib/JsonView.php. What I want to do is to
Import the JsonView.php file in another file in project_root/app/View/CustomJsonView.php. (I think I could use App:import, would it be right ?)
Choose this file as the custom in requestHandler like this:
public $components = array('RequestHandler' => array( 'viewClassMap' => array('json' => '/right/way/to/this/file/CustomJsonView', )));
But how do I write the right way for this file ?
I also saw this one https://book.cakephp.org/2.0/en/core-libraries/components/request-handling.html#RequestHandlerComponent::viewClassMap
but there is no explanation about the right paths to the file. My CakePHP version is 2.4.4 .
You are not supposed to pass full paths, but "short classnames", just like shown in the linked example, where ApiKit.MyJson refers to the MyJsonView view class in the ApiKit plugin, which could be located in app/Plugin/ApiKit/View/MyJsonView.php.
If you follow the conventions and place your CustomJsonView class in app/View/CustomJsonView.php as shown in the docs, you then just pass CustomJson as the short classname in the request handlers viewClassMap option.
Whether you use App::import() or just require to include the /lib/JsonView.php file, is up to you, both works. In any way you must make sure that whatever you are importing there doesn't clash with existing class names (JsonView is a kinda reserved name as it already exists in the core), and that it is either following the CakePHP view class naming conventions, or you must extend it.
See also
Cookbook > Views > Creating your own view classes
Cookbook > Core Libraries > General Purpose > App Class> Loading Vendor Files

Create folder/Sub-folder in Code-igniter

I am a Drupal dev and new to code-igniter or any such php frameworks.
Now i have to modify an existing application done on codeigniter and the structure must be as follows:
example.com/motors
example.com/motors/car-for-sale
example.com/motors/car-for-rent etc.
Before it has only one url example.com/motors and i want to create more urls as mentioned above.
In the application\views\content folder i have the following structure:
application\views\content\motors.php
application\views\content\motors
application\views\content\motors\car-for-sale.php
In the application\controller folder i have the following structure:
application\controller\motors.php
application\controller\motors\motors.php
application\controller\motors\car-for-sale.php
I want to get the url example.com/motors & example.com/motors/car-for-sale from the files resides in the motors folder.Also how can i set a default file to load when i open example.com/motors?
You can't have a (controllers) directory that matches the name of a controller class at the same level. That is, since you have a controllers/motors.php, the files under controllers/motors/* will never be reached.
Instead (and this is the answer to your second question), you should set the default_controller name and rename controllers/motors.php to controllers/motors/<default_controller>.php.
Note that the default_controller setting points to a controller name (not a file location) and is applied to all directories. That is, if you set it to 'Default', then controllers/Default.php will be used when you open http://domain.tld/ and controllers/motors/Default.php will be used if you open http://domain.tld/motors/.
Also, your controller names MUST start with a capital letter, so default.php would be incorrect and should be Default.php instead. This might be working for you on Windows right now (because of its case-insensitive file system), but as soon as you upload your site to a Linux (or other UNIX-based) host, any classes with file names that don't start with a capital letter won't work.
It looks like you're trying to build a CodeIgniter site with a completely different paradigm from what it is designed around.
The structure you are after can be set up using the routes.php file within application/config
In there, you can set routes to go to any location needed, so for you, something like:
$routes['motors/cars-for-sale'] => 'motors/cars_for_sale';
$routes['motors/cars-for-rent'] => 'motors/cars_for_rent';
Then in application/controller you'd have a Motors.php file, which starts:
class Motors extends CI_Controller{
And also has the functions cars_for_sale and cars_for_rent
The mappings in routes sets this to link together.
In order to get the views you want for any given route, in the controller function, you'd have:
$this->load->view('path/to/view/file', $array_of_data); // view path does not need the .php extension
I'd recommend having a look and possibly even a follow through of the CodeIgniter tutorial in their documentation

CodeIgniter HMVC user specific module structure

Im using CI-HMVC stock.
Id like to have a module structure like this:
application
modules
userabc
moduleA
controllers
models
views
moduleB
...
userDEF
moduleC
...
Is this an incorrect way of module organization? Is there another common way of doing something like this?
Im wanting to seperate the users module folders and use them in a URL like this:
userABC.domain.com/module/controller/method
userBCD.domain.com/module/controller/method
You can use that structure if you want, but keep in mind the following:
You must define each user's directory as a module location:
// application/config/config.php
$config['modules_locations'] = array(
// Absolute path // Relative from default application dir
APPPATH.'modules/userABC/' => '../modules/userABC/',
APPPATH.'modules/userDEF/' => '../modules/userDEF/',
APPPATH.'modules/userGHI/' => '../modules/userGHI/',
// etc.
);
You might be able to do this dynamically, but remember that config.php is loaded pretty early so you may need a pre_system hook.
The other thing, which is important if you want all users' modules accessible regardless of which subdomain is active: Order matters!
If userA has a module called "blog" and so does userB, only userA's will ever get loaded (assuming you define userA's module path first). If you're certain no two modules will share the same name, this won't matter as much, but you may suffer a performance hit as the loader will go through the whole stack of module locations until it finds the requested one.
It sounds like you should define a single module_location depending on what user's site is loaded (the subdomain). Something like:
// Get this value dynamically (not sure how you need to do it)
$current_user = 'userABC';
$config['modules_locations'] = array(
APPPATH.'modules/'.$current_user.'/' => '../modules/'.$current_user.'/'
);
Adding submodules in codeigniter you need to defile moudules direcotry in config file like this.
$config['modules_locations'] = array(
APPPATH.'modules/' => '../modules/backend/',
APPPATH.'modules/frontend/' => '../modules/frontend/',
);

Get to parent folder in codeigniter

I'm putting an DOMpdf creator in my Codeigniter application, but now i need to get an require_once to the parent folder. Somehow Codeigniter doesn't allow me to do this the "normal" way.
This is my require_once
require_once("../dompdf_config.inc.php");
How can I make this work in Codeigniter?
Try something like:
//APPPATH gives you path till application/ folder
require_once(APPPATH.'your_DOMpdf_file_path');
Hope it helps
To load a config file in CodeIgniter, you can call the $this->config->load() method.
To load one of your custom config files you will use the following
function within the controller that needs it:
$this->config->load('filename');
Where filename is the name of your config file, without the .php file
extension.
If you need to load multiple config files normally they will be merged
into one master config array. Name collisions can occur, however, if
you have identically named array indexes in different config files. To
avoid collisions you can set the second parameter to TRUE and each
config file will be stored in an array index corresponding to the name
of the config file. Example:
// Stored in an array with this prototype: $this->config['blog_settings'] = $config
$this->config->load('blog_settings', TRUE);
Please see the section entitled Fetching Config Items below to learn
how to retrieve config items set this way.
The third parameter allows you to suppress errors in the event that a
config file does not exist:
$this->config->load('blog_settings', FALSE, TRUE);
http://codeigniter.com/user_guide/libraries/config.html

CodeIgniter Include of Vzaar Library

I am trying to load the Vzaar libraries into Codeigniter. Should these file go into the libraries directory. If so there are multiple files, which one would I make the call to from my controller. I believe the main library file is Vzaar.php. Therefore should my call be
$this->load->libraries('Vzaar');
Presumably this library is a standard PHP class called Vzaar? If so, then yes, what you've said is correct. Put Vzaar.php into your application/libraries folder and load it up at any time using $this->load->libraries('Vzaar'). Then you can use it by doing $this->Vzaar->myFunction(). For more info, check out CI's awesome user guide:
http://codeigniter.com/user_guide/general/creating_libraries.html
Yes, $this->load->libraries('Vzaar') followed by $this->Vzaar::function_name() should do the job, since it's just a PHP class and it has all require_once instructions inside of it already.
Evi Skitsanos,
vzaar API Head of Support,
email: evi#vzaar.com | twitter: http://twitter.com/skitsanos

Categories