my question is simple:
How do I add an API and/or 3rd party
library to my ZendFramework
application in a way which makes it
possible to access it in a
controller
Here is a blog post detailing how to achieve this: http://blog.keppens.biz/2009/05/add-your-own-library-to-your-new-zend.html
Alternatively, if you don't want to tweak the application.ini file, you can do it through your Bootstrap class. Add this function to Bootstrap:
protected function _initAutoload() {
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('MyCode_');
}
Then in the "library" folder, you would add a folder called "MyCode". This folder should be parallel to the "Zend" folder. Naturally you should change "MyCode" to reflect the name of the library you're adding.
I should note that by using the above method, I'm assuming the code uses the PEAR naming scheme (just like ZF).
Related
I'm taking over a project where the original developer customized JomSocial and wrote directly to:
components/com_community/templates/jomsocial/layouts
And created there folders aswell as wrote into files like base.php and so on.
The current customized version of JomSocial is 4.0.9 and I need to update to the latest - 4.4.5, the Joomla version is - 3.8.1
I understand I need to create an override folder in my template folder like that for an example of the base.php file for each of the changes:
/templates/yourTemplateName/html/com_community/layouts/frontpage/base.php
But in order to keep the changes the developer made and have the latest version of JomSocial while not hindering further updates like in this situation I would have to if I'm not mistaken:
Separate the changes from the core files he made on 4.0.9.
Create folders and files in my templates folder for each core files I need to customize and have them include only the changes the developer made with an updated version of 4.4.5.
Am I getting this right? is there another way to do it? any help would be greatly appreciated.
You're correct on how to override the layout files in a template. Unfortunately it ties your overrides to a particular template - installing a different template and setting it as default will lose your overrides.
You can override any other file in a component by using a plugin and intercepting the call to the component's controller, substituting your own. You would have to copy all of the Jomsocial files between the entry point (controller) and the file you want to apply changes to with this approach, which is probably why the original developer just overwrote the core component files directly (can't safely update with this approach anyway, without checking for changes to the code you've copied from the core component files into your override). This approach would look something like:
class plgSystemComSocialOverride extends JPlugin {
public function __construct(&$subject, $config = array()) {
parent::__construct($subject, $config);
}
public function onAfterRoute() {
$app = JFactory::getApplication();
if('com_social' == JRequest::getCMD('option') && !$app->isSite()) {
require_once(dirname(__FILE__) . DS . 'comsocialoverride' . DS . 'my_jomsocial_controller.php');
}
}
}
I think you could combine the above approach with manipulating Joomla's class loader, to avoid having to directly copy Jomsocial files in order to change their include statements to your override files. In this approach, you'd override the controller as above and call the Jomsocial controller from that overridden controller. But before you do that, you'd add your overridden base.php or whatever to the class loader. I'm not sure if the new version of Jomsocial is using autoloading or not (i.e. import() instead of include()).
Say I want a constant, a function or a class to be available in all my models, views and controllers code (within a particular web site (application)). Where can I put them? I don't mind importing them explicitly in every file I need them in but I don't know how (simple require_once does not seem to work).
You can put them in the vendor folder (in application/vendor or modules/MOD/vendor). Then you can load it like this:
require Kohana::find_file('vendor', 'folder/file','ext');
You can read up more on this in the user guide
Now, it should be stated you should in general not use functions or globals.
I declare Constants in Bootstrap.php and create my own Helpers for general functions under application/classes/Helpers.
If you need to integrate a third party library into Kohana or want to make code available to other Kohana users consider creating a module instead.
You can define all your constants in new php file and place it in the application/classes directory. In your Template controller or in the main controller like Welcome or Website, just place the code in the __constructor()
require_once Kohana::find_file( 'classes', 'filename' );
I suggest you to put your constant variables in the bootstrap.php file because this is loaded by the framework before every request.
You can simply put your classes in the root of the classes directory, the framework will find.
This worked well for me...
In application/config/constants.php:
define('SOME_COOL_CONSTANT', 'foo');
return array();
In index.php:
Kohana::$config->load('constants');
SOME_COOL_CONSTANT should then be available globally.
Which folder should I put strategy objects, or any objects that are not domain models? I'm using Zend Framework, if that matters. Appreciate it!
I would use the recommended folder structure laid out here in the Zend Programmer's Reference Guide and place them into the modules folder.
Two pretty standard options:
Place this code into the library folder. Typically a file library/App/SomePackage/SomeClass.php wouldcontain the class App_SomePackage_SomeClass. Just add the line autoloadernamespaces[] = "App_" into your configs/application.ini file.
Create a new folder inside your application folder and configure a Zend_Loader_Autoloader_Resource (or its extended class Zend_Application_Module_Autoloader) with appropriate appnamespaces, paths, and prefixes.
Using this second approach could go something like this:
protected function _initResourceLoader()
{
$resourceLoader = Zend_Application_Module_Autoloader(array(
'namespace' => 'Application_',
'basePath' => APPLICATION_PATH,
));
$resourceLoader->addResourceType('strategy', 'strategies', 'Strategy');
}
Then a class named Application_Strategy_SomeClass would reside in the file application/strategies/SomeClass.php.
Noe that using Zend_Application_Module_Autoloader - instead of the more generic parent Zend_Loader_Autoloader_Resource - will give you a standard set of autoloader mappings for models, forms, services, view helpers, etc.
I am currently using CodeIgniter to support one site, whose main purpose is to display multiple editable tablekit tables, and handle the AJAX that arrives when you edit them. A kind of PHPMyAdmin [VERY] Lite. It has a number of core helpers and controllers, which run the main workings of the site, mixed in with some site specific controllers and helpers.
I would like to restructure my site so that I can reuse the core code-base in another site. However, I would still like to be have some default controller functions and some cutsom functions in the same controller; i.e. in a system file somewhere:
class My_core extends Controller{
/*
Lots of base functions
*/
}
and on one site:
class site_1 extends My_Core{
/*
Site specific functions
*/
}
Then on the other site:
class site_2 extends My_Core{
/*
Site specific functions
*/
}
Does anyone have any guidance on how I can do this?
Thanks,
Lemiant
If you are using CodeIgniter 2.0 you can achieve most of this with with packages. They will let you load helpers, libraries and models from anywhere, so in each application simply configure a package to be loaded from that shared folder.
As for core libraries (which MY_Controller will be) you will have to implement your own __autoload() function:
http://php.net/manual/en/language.oop5.autoload.php
You can put an autoloader at the bottom of config.php. As long as it is checking the correct folders (local first, then the shared folder structure) it should all work pretty nicely.
I don't know if this is still helpful to you but heres what I've done.
say I have 2 websites, palladium.com and osmium.com.
my file tree looks like this
/var/www/system/ (the CI system folder)
/var/www/palladium/application
/var/www/palladium/public/index.php
/var/www/osmium/application
/var/www/osmium/public/index.php
inside those index.php files are lines that define where /system/ is stored. I've got that set to
$system_folder = "../../system";
Now inside /var/www/system/libraries i have a file named MY_TestClass
<?php
class MY_TestClass {
public function MY_TestClass() {
echo "this is a test of the emergency broadcast system";
}
}
From anywhere inside BOTH palladium.com and osmium.com i can call
$this->load->library('MY_TestClass');
and "this is a test of the emergency broadcast system" will show up.
Just need get some vals located in application.ini(main ini) in the Controller plugin
I know I can create an instance of Zend_config_Ini but would like to use the existing resource (application.ini is already used in Front Controller)
Use Zend Registry
// when loading the config
Zend_Registry::set('config', $config);
// later, somewhere
$config = Zend_Registry::get('config');
Try:
Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
If you now that you will need your configuration values from application.ini in few places around your whole application, you could read it up in bootstrap and store it in registry:
$appConfig = new Zend_Config_Ini('path/to/application.ini');
Zend_Registry::set('applicationConfig', $appConfig);
then later you can always access it:
Zend_Registry::get('applicationConfig')->someValue;
For reasons of testability and decoupling I would not use the Zend_Registry approach. In my opinion the plugin should not know anything about Zend_Config, but instead all options that are needed in the plugin should be injected from outside.
For example, if you want to use the config option "MyOption" in the Plugin Controller_Plugin_MyPlugin you could inject the needed parameters in the Bootstrap class as follows:
public function run()
{
$sopPlugin = $this->getResource('frontcontroller')->getPlugin('Controller_Plugin_MyPlugin');
$sopPlugin->setMyOption($this->getOption('MyOption'));
parent::run();
}
The advantage is that with this solution you avoid dependencies to Zend_Registry (which in fact is just an euphemism for a global variable) in your plugin code.