CodeIgniter core library extensions within subfolders - php

I am curious if it is possible to organize library extensions within subfolders of the libraries directory within codeigniter application.
For example lets say we want to extend the 'Form_validation' library. The default way to extend this core library would be to create a class with the same name prepending 'MY_', and saving it as follows:
/application/libraries/MY_Form_validation.php
Now let's say we want to keep all of our extended libraries we have created within a subfolder called 'extensions' like such:
/application/libraries/extensions/MY_Form_validation.php
When trying the above the extended library is ignored and the default Form_validation class is loaded. Once this happened my first thought was to modify the subclass_prefix within /application/config/config.php to include the 'extensions' directory, however this resulted in an error stating:
Non-existent class: Form_validation
Is it possible to organize your extended libraries in this fashion within codeigniter? Does anyone know how it is done?

If It's have a sub-folder
$this->load->library('extensions/MY_Form_validation');
and after you can access like..
$this->MY_Form_validation->your_function_name();

Related

Can't import Vendor class into Component in CakePHP 2.5.1

I've been looking at all the related questions of this topic and none of the solutions provided (usually App::import() ) have worked for me, maybe because I have a different configuration, which is the following:
I have a regular cake installation which loads components from an external folder (so outside this installation). That works perfectly, even for the component I'm trying to use now (it works fine until I try to load the Vendor class). This Vendor class I want to have it outside the Cake installation as well (same as with the components). This is how this installation looks:
[root]
.......[shared_resources]
......................................[CakePHP]
........................................................[Components]
..............................................................................MyCustomComponent.php
........................................................[Vendor]
....................................................................[MyVendor]
......................................................................................MyVendor.php
......[MySite]
................... [cakephp typical folder structure]
In my site's bootstrap.php file I have App::build(array('Controller/Component' => array(dirname(ROOT) . '/shared_resources/CakePHP/Component/'))); in order to be able to load that component in any controller, which works fine, any component I put in that folder loads and works without issues.
Now, I'm trying to load the MyVendor class in the MyCustom component, but I can't get it to work, no matter what I try I keep getting class not found errors when trying to instantiate it.
I've tried using php's and Cake's require(), import(), App::import() and App::uses() with all possible combinations of paths (absolute and relative) without any success, puttin them before the declaration of the component class and inside the method that actually uses the vendor class. The last one I've tried is App::import('Vendor', '/absolute/path/to/shared_resources/Vendor/MyVendor/MyVendor.php'); for example.
I've also tried using App::build(array( 'Vendor' => array(dirname(ROOT) . '/shared_resources/CakePHP/Vendor/'))); in the bootstrap file, like with the components.
I don't know what else to try, any help would be much appreciated!!!
Oh, I've check with PHP that the file Vendor class file exists in that path too.
According to your folder structure,
To access your MyVendor.php, you should write like this
App::import('Vendor', 'MyVendor', array('file' => 'MyVendor/MyVendor.php'));
For more information, read http://book.cakephp.org/2.0/en/core-utility-libraries/app.html

laravel 4 create module and put controller , module and view inside module

I have worked on yii framework before and I had possibility to create module folder and inside this put eg: news module which was included controllers, views and module
I'm new in laravel and trying to create same thing MODULE
i tried the following inside routing
Route::get('/news','/modules/news/controllers/NewsController#index');
file exist but i'm getting
ReflectionException
Class /modules/news/controllers/NewsController does not exist
why ? what i'm doing wrong ?
The Route::get() function is looking for a (auto)loaded Class, not for a file on the disk to load, which is why you're getting these errors.
It's more Laravely (Laravelish?) to include:
Controllers in the /app/controllers/ directory
Views in /app/views/ directory
Models in the /app/models/ directory
And if you are starting out with Laravel, this might be the best way to get started. The autoloader knows where to look for your classes then, and everything gets handled automatically for you.
With the NewsController situated in /app/controllers/ you can do this:
// no need to start the route location with a slash:
Route::get('news', array('uses' => 'NewsController#index'));
You can "package" up functionality using Laravel's Packages, but it would be better to check out the excellent docs and then come back with specific questions..
Put your Laravel controllers in app/controllers, since that directory gets autoloaded, and it is where Laravel expects controllers to be.
Then you can use routes like this (example straight from the docs at http://laravel.com/docs/controllers#basic-controllers)
Route::get('user/{id}', 'UserController#showProfile');

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/

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.

In which folder to place the classes that I extend in the Kohana 3.1 framework?

I am new to the kohana php framework. In the modules folder in kohana 3.1, there are many empty files extending the existing classes. Should I write my code in those empty files?
If yes, do I have to make any changes in bootstrap?
If not, where should I place these files? Should they be in a subfolder inside the Application directory or inside the modules directory?
Which all files will I have to copy from the modules to application?
Those empty files you see are aliases created for the class. An example would be the Cookie class, declared as so:
class Cookie extends Kohana_Cookie {}
It's just another way for you to refer to the real class, in this case Kohana_Cookie, without having to type all that out.
So when you use something like Cookie::salt($name, $value) you're really just using Kohana_Cookie::salt($name, $value).
If you want to extend a class, you can drop the files into your application/classes folder and go from there.
check out the docs at http://kohanaframework.org/3.1/guide
especially: http://kohanaframework.org/3.1/guide/kohana/files
you can extend classes in the folder: application/classes/.. or modules//classes/..

Categories