I have been using the CodeIgniter i18n library by Jérôme Jaglale (http://maestric.com/en/doc/php/codeigniter_i18n), which works great for my project.
But since I need to write separate modules, I recently added CodeIgniter Modular Extensions ( https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc ) and the navigation breaks.
How can I solve this issue please, I would love to use both the i18n library & Modular Extensions.
I think my application navigation fails to work because i18n library introduces adds a language segment in the site url, in my case my url is localhost/index.php/en/home and after adding Modular Extensions, my navigation/links stop working.
Thank you in advance.
Recently, I try to use HMVC with i18n and have similar problem. Below is my solution.
1.first you need to go to here HMVC select "Branches" to download HMVC extension, don't download the one on github it might not work.
2.Unzip HMVC extension inside the folder copy two files "MY_Loader.php" and "MY_Router.php" from core folder to Codeigniter's "application/core" after that copy "MX" folder from "third_party" to Codeigniter's "application/third_party". By this point your HMVC is installed, however it will not work because i18n cause the problem so if you run your website it might not display.
3.You need to get new version of i18n which support both HMVC and none HMVC, the old version of i18n seems not support HMVC. Go to here i18n download it and take time to read the description on github.
4.Before this step I suggest you to backup "application/core/MY_Config.php" and "application/core/MY_Lang.php" in case something goes wrong you can reverst back. Unzip i18n inside folder copy file "language.php" from config folder to Codeigniter's "application/config", copy two files "MY_Config.php" and "MY_Lang.php" from core folder to Codeigniter's "application/core", finally copy "MY_language_helper.php" from helpers folder to Codeigniter's "application/helpers". So far you have new i18n installed, but you need to configure it to make it work, otherwise you might get error message.
5.Open "application/core/MY_Config.php" and replace the line require_once APPPATH . "libraries/MX/Config.php"; to require_once APPPATH . "third_party/MX/Config.php"; then open "application/core/MY_Lang.php" replace the line require APPPATH . "libraries/MX/Lang.php"; to require APPPATH . "third_party/MX/Lang.php";. Why? because it point to the wrong directory, the MX folder is located in "third_party" not "libraries" in case you don't know, if you don't change it you might get error message.
6.To add new language(Not create language file) you need to open "application/config/language.php". You see at top the code block with comment says "Supported Languages" there already have English and Russian language configure for you just need to copy the template and change to the language you want, it's very easy. Be aware folder's name must exactly the same as folder in "application/language".
7.According to i18n github description you need to add these line
$route['^(en|de|fr|nl)/(.+)$'] = "$2";
$route['^(en|de|fr|nl)$'] = $route['default_controller'];
to the "application/config/routes.php". Be aware this line $route['^(en|de|fr|nl)/(.+)$'] = "$2"; in old i18n probably is $route['^(en|de|fr|nl)/(.+)$'] = "$1"; the difference is "$1" have to change to "$2", otherwise you will got problem.
8.To create language file is same as the method you did in old i18n. Now test your website with multi-language to make sure everything work fine.
9.Create your module. How? Create a folder name "modules" inside Codeigniter's application folder, inside modules folder you can start to create your module. That's say you want to create a module call foo, you just need to create a folder name it "foo" and then inside foo folder you can create three folders controllers, models and views. Create a php file with name foo with the code below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Foo extends MX_Controller
{
public function index()
{
echo "<h1>class foo this is module test</h1>";
}
}
Enter url to run your module, if you see "class foo this is module test" then it work.
Remember module class must extends from MX_Controller.
If you still encounter any problem just ask.
Related
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
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/
Is it possible to load a single plugin from outside the cake root using App::build()?
If I do:
App::build(array(
'Plugin' => array('/full/path/to/plugin_dir/')
));
..I can load a whole directory of plugins but what if I only want to use one of them? Is this possible? (I get errors when using a path to the specific plugin directory).
(P.s. This is cake version 2.1)
From the bootstrap.php in a baked application:
CakePlugin::load('DebugKit'); //Loads a single plugin named DebugKit
You have to add the directory with the plugin in it via App::build() and then the plugin itself with the method above.
While using "App::build()" you are not "loading" all the files in there. You are defining the paths where cake searches for php to include in case it has to.
Nothing will be loaded if it is not need since Cakephp 2.0. Everythings is about lazy loading ^^
How this is, what you are searching for ;)
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.
On my local setup I have a load of different CakePHP websites. I'm using a Mac so the folder structure is something like ~/Users/cameron/Sites/sample-website and then within each of these websites I will have the typical Cake folder and App folder.
What I would like to do is have just a core cake folder and then have ALL the sites pull from that one cake core so I don't have the same stuff several times over. I have been reading some tutorials on the web: http://rickguyer.com/cakephp-one-core-many-apps/
So I have my cake folder here: ~/Users/cameron/Sites/cake-1.3/ and then my site here: ~/Users/cameron/Sites/sample-site/ and in this folder I have the usual app folder and htaccess to tell it where to find webroot etc.
Now I have edited the index.php file inside webroot like the tutorial BUT have only changed one line because I haven't moved my files OUTSIDE of the App folder like he does. So the only like I have changed is as follows:
if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
define('CAKE_CORE_INCLUDE_PATH', '..'.DS.'..'.DS.'cake-1.3');
}
As far as I can tell that is correctly looking two directories up and finding a folder called cake-1.3 however it just gives a error 500?
Any ideas what the problem is? Thanks
EDIT:
Even doing this doesn't work???
Which If I echo: echo CAKE_CORE_INCLUDE_PATH; gives /Users/cameron/Sites/cake-1.3 and if I paste that in the address bar it loads up the cake folder so it's definitely the correct folder structure JUST it doesn't like looking at cake outside of the main url?
if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
define('CAKE_CORE_INCLUDE_PATH', DS.'Users'.DS.'cameron'.DS.'Sites'.DS.'cake-1.3'); echo CAKE_CORE_INCLUDE_PATH;
}
You are right on the money with:
define('CAKE_CORE_INCLUDE_PATH', DS.'Users'.DS.'cameron'.DS.'Sites'.DS.'cake-1.3');
Just make sure that Users sits in root. In other words, when you go to terminal you can get to this directory by typing: cd /Users/cameron/Sites/cake-1.3
It looks like you may be on a MAC. If so, your linking is correct. Most of the time what I find is you have done a copy paste of the app directory and it does not get the .htaccess files. I would check those first. But here is a comprehensive list of what you should verify:
Make sure the host is pointing to
the correct directory
(/Users/cameron/Sites/sample-site/)
Verify mod_rewrite is in fact on.
Verify you have copied the .htaccess
file in both the
/Users/cameron/Sites/sample-site/
and the
/Users/cameron/Sites/sample-site/webroot
directories.
Confirm that the
/Users/cameron/Sites/cake-1.3/
directory has a directory called
cake in it that contains the core.
Once all of this is confirmed, you will be good as gold!
Happy Coding!
UPDATE:
When the index.php file looks for the cake core, it will look for a directory inside the location you are pointing to for another directory called cake. So in your case:
define('CAKE_CORE_INCLUDE_PATH', DS.'Users'.DS.'cameron'.DS.'Sites'.DS.'cake-1.3');
You must have the cake directory inside /Users/cameron/Sites/cake-1.3. Your directory structure will look like:
/Users/cameron/Sites/cake-1.3/cake
/Users/cameron/Sites/cake-1.3/cake/libs
/Users/cameron/Sites/cake-1.3/cake/config
/Users/cameron/Sites/cake-1.3/cake/console
etc.
CakePHP 3.0+
In CakePHP 3.0+ this configuration is moved out of webroot/index.php to App/Config/paths.php
If you have access to your php.ini, you can add the path to Cake core there. Doing it this way means you don't have to change webroot/index.php at all. Example in php.ini:
include_path = ".:/usr/local/lib/php:/home/something/phpinc/cakephp2/lib"
According to the CakePHP 2.x docs, this is the recommended way to share the Cake core (assuming you have access to your php.ini).
You can have only one cake core but you must have one app folder (containing MVC) by site.
Is this a misunderstanding of the folder structure of CakePHP?
From the docs (CakePHP folder structure):
The app folder will be where you work your magic: it’s where your application’s files will be placed.
The cake folder is where we’ve worked our magic. Make a personal commitment not to edit files in this folder. We can’t help you if you’ve modified the core.
So the cake folder shouldn't change between all of your uses, therefore you have 1 copy. You can always change some of the functionality of the core by making your own changes in the app folder i.e. extending.
There is no need to edit index.php.
Just put an alias (or link in UNIX) to your cake folder in each of your sites folder. Works perfectly. Same goes for plugins and vendors folder.