I have a class Called MyClass. It lives in app/Lib/MyDir/MyClass. I'd like to use App:uses() to load it, but can't get it to work.
In CakePHP 1.3 I would load it via:
App::import('Lib', 'MyDir/MyClass'); //this still works in CakePHP 2.1
In CakePHP 2.1 I'm trying to do:
App::uses('MyClass', 'Lib/MyDir');
When I try to 'new' up MyClass I get Class 'MyClass' not found.
Is it not possible to use App::uses on custom Lib classes? I can't continue to use App::import() because if 'App::import('Lib', 'MyDir/MyClass');' appears 2x in the code path I get a 'Cannot redeclare class' error in lib/Cake/Core/App.php on line 531
what am I doing wrong?
Edit: so if I do App::uses('MyClass', 'MyDir'); it works. Not sure if thats how its supposed to behaive, but reporting bug.
did you try
App::uses('MyClass', 'MyDir');
? since "Lib" itself can be seen as a base directory
PS: you are even supposed to group everything inside Lib in subfolders (packages) similar to the core.
Related
I've created a video about one of the major problem that I have. I need to understand the file loading architecture, how it's done when writing a class in WP plugin development (how one class knows about the existence of another class), and how this compares with WP MVC file loading architecture. It's one big question, and I went through the various smaller questions that helped me arrived at the question summary in the video in the drive link below. I'm putting the plugin code in the Google Drive folder also: https://drive.google.com/open?id=1JVSSlkSJ5pCfNojRh6jen3ax2w-HZr5d
WP side:
plugin.php has this line: $wp_filter[ $tag ]->do_action( $args );
$tag = 'init'
This calls WP_Hook->do_action, then WP_Hook->apply_filters
WP MVC side:
This then calls MvcLoader->init, which then calls the needed load_controllers/libs/models/settings/functions (only the files under the bolded folder names will be loaded)
load_controllers will use the file_includer and make all files under the [plugin app path]/controllers be required
This means that any class under the specified folders above will be auto-required for the plugin that depends on WP MVC [tested]
This also explains why if one create a helper function, following the documentation, one would put the class under helper folder, but that doesn't get auto-loaded and one will get class not found error (because helpers doesn't get loaded/not in the list above)
Upon a closer inspection, it looks like only classes extending MvcController can load any helper files (base class MvcController is the only class that has load_helper() function). The helper files need to be named xyzHelper, under helpers folder, and needs to extend MvcHelper. In the child controller class, call $this->load_helper('xyz') and the base class will automatically try to load the helper class from the helpers folder. The controller class will now have a helper object named xyz (the class name, including underscores, without the _helper suffix). One can now have access to the helper class through the controller class (in the view classes also, etc. Used like $controller->xyz->function()) [tested]
How to get the above info:
Activate an example plugin from WP MVC and navigate to a public view
Put the breakpoint in mvc_file_includer.php require_file() [I actually just run the debugger without any breakpoint and there was a deprecated function warning at the above function. I didn't think to put a breakpoint at that function previous to seeing this, and I didn't know where to put the breakpoints previous to this realization]
Look at the stack trace and try to understand
Also, in WP MVC, there are other calls that will automatically include files
An example is render_view() - This will either call the controller's, or the helper's, render_view(), which will use the file_includer to find the desired file path, and call require on that file path. Therefore, it's more like this is a dynamic-loading of files that is being used.
Now, how did MvcLoader->init() get triggered?
In the main plugin's file: wp_mvc.php, depending on if it's in the admin/public functionality view, wp_mvc_load_global_functionality is called with the needed parameter. One of the actions in that function is add_action('init', array($loader, 'init')), which triggers the init hook for the MvcAdminLoader, which is a child of MvcLoader [tested by commenting out the add_action and it no longer works]
I ran into a problem, while coding around in CI 3.0.3 and latest HMVC Extension. I'm autoloading some libraries, helpers and much more. The Session library too. Now i have a few controllers all working fine, except one.
In that controller i use this code:
modules::run($module->module . '/' . $module->controller . '/' . $module->method, unserialize($block->configuration));
While using this static function i got the error:
Unable to locate the specified class: Session.php
The log says that the Session class is initialized.
INFO - 2015-12-01 09:41:40 --> Session: Class initialized using 'database' driver.
Any ideas why i got this error?
EDIT: -----------------------------------------------------------------------
To prevent stupid answers as below ...
In CI we can’t call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate controllers. It can be done with libraries, or with this “Modular Extensions HMVC” contribution.
The differences between using a library and a “Modular HMVC” HMVC class is: 1. No need to get and use the CI instance within an HMVC class 2. HMVC classes are stored in a modules directory as opposed to the libraries directory.
EDIT 2: ---------------------------------------------------------------------
For further questions:
Yes, the Session Library is the problem
Yes, only in combination with this HMVC Plugin
Yes, only if you use the static method modules::run();
No, i need this method for simulating a controller in a controller
Simple.
If you want to load a controller in a controller use:
Modules::load();
or outsource the Modules::run(); into a view that is loaded by the main controller.
Simple.
Im building a custom CMS and have setup an autoloader, and have adapted use of namespaces. For the most part things are loading properly, but in certain cases PHP reports that it cannot find the class, the class file has been included.
Once a file is included (using require), it should be instanced as well.
The parent controller is instanced, then the child controller attempts to instance a few of its own dependencies.
$this->auth = new \Modules\Auth\RT\Auth();
This will look for a file at /modules/auth/rt/auth.php, and it does and the class is instanced properly.
The namespace of Auth is:
namespace Modules\Auth\RT;
The auth class tried to load its own dependencies, a model in particular.
$this->auth_model = new Models\ModelAuth();
Here the file to be included is at /modules/auth/rt/models/modelauth.php
It is included successfully, but this is where PHP says I cannot find this class.
Fatal error: Class 'Modules\Auth\RT\ModelAuth' not found in /Users/richardtestani/Documents/ShopOpen-Master/shopopen/modules/auth/rt/auth.php on line 12
What would cause the class from not being instanced even though the file is included?
Thanks
Rich
try this:
$this->auth_model = new Modules\Auth\RT\Models\ModelAuth();
try this:
$this->auth_model = new \Modules\Auth\RT\Models\ModelAuth();
OR
$this->auth_model = new Models\ModelAuth();
when you are in this namespace \Modules\Auth\RT
There was a Missing \ , so the code trys to include the namespace twice;
FOR REAL ;)
I am trying to load a custom exception class that I created according to the instructions here:
http://codeigniter.com/user_guide/general/core_classes.html
MY_Exceptions.php is stored at application/core/
Somehow, when I try loading it, I keep getting this error:
Fatal error: Class 'MY_Exceptions' not found in C:\xampp\htdocs\xampp\facebook\application\models\campaign_model.php on line 29
Based on the instructions, it doesn't say I have to autoload the class or anything. What am I doing wrong?
MY_Exception.php is stored at application/core/
Name the class and file MY_Exceptions with an s
You do not need to autoload or manually load anything in the core directory, nor should you. They are required classes for CI to run that are automatically loaded.
For creating core classes, use this documentation instead: http://codeigniter.com/user_guide/general/core_classes.html
Keep in mind that when calling the class you will use the original class name. Let's say you have created MY_Input. Example:
$this->input->post(); // Do this
$this->my_input->post(); // Don't do this
To understand the why and how, see system/core/Common.php function load_class.
Looking at the docs now, I agree that this should probably be highlighted.
It's trying to find MY_ExceptionS.php, not MY_Exception.php.
We are finding cases where we get the following 500 error:
File xyz.php does not exist or class "xyz" was not found in the file at () in SF_ROOT_DIR/lib/vendor/Zend/Loader.php line 107 ...
where xyz ==
Memcache (when trying to use symfony cc on the command line)
or
sfDoctrineAdminGenerator (when using an old-ish AdminGenerator-generated CMS page).
We use Propel, but Loader.php is trying to load classes used only for Doctrine.
Currently I am using a filthy hack where I request Loader.php to check if the file is either of these two cases, and if so simply return rather than trying to load it. Obviously, this is unacceptable longer term.
Has anybody encountered this, and how did you solve it?
Edited to add:
We have:
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
// for compatibility / remove and enable only the plugins you want
$this->enableAllPluginsExcept(array('sfDoctrinePlugin'));
}
}
And we have a propel.ini file in our top level config directory. This has only started in the past four weeks or so, and we've had a stable build for over a year now. I'm pretty sure Doctrine is totally disabled.
The only thing I can think of is to make sure Doctrine is disabled in your project configuration...
Could this be a conflict between Symfony's autoloader and Zend's? Try adding the following to your ProjectConfiguration.class.php:
set_include_path(sfConfig::get('sf_lib_dir') . '/vendor' . PATH_SEPARATOR . get_include_path());
require_once sfConfig::get('sf_lib_dir').'/vendor/Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
I use Zend Lucene using the above and it works. If that doesn't help, perhaps something in these Zend and Symfony slides may help.
Clear Cache :) And make sure your index.php have the right include file.