class My_Plugin extends Zend_Controller_Plugin_Abstract
{
public function init()
{
print 'this is working just fine';
}
public function preDispatch( Zend_Controller_Request_Abstract $request )
{
Zend_Debug::dump($request);
print 'why is it not working';
exit;
die(':('); // not dieing either
}
}
The plugin is registered in /configs/application.ini file. ZF does see it, because init() function works perfectly fine. But nothing I put into preDispatch seems to work.
P.S. the only purpose of this plugin is to determine what language is used from the parameter in URL, and set Zend_Locale to it. So that I won't need to do it in any controller or view ever again, instead relying on Zend_Locale, Zend_Translate, etc. But I can't do that in plugin's init() and preDispatch() doesnt work at all :/ The lack of proper documentation for ZF starting to drive me crazy
The problem was I needed to add one line to application.ini:
resources.frontController.plugins.myplugin = Plugins_My_Plugin
Everything works now.
Related
I have one controller in a subfolder which works perfect:
route:
Route::resource('json/stock/equipmentImages', 'Stock_EquipmentImageController');
getting its class from controller/stock/EquipmentImageController.php
which has a class definition:
class Stock_EquipmentImageController extends \BaseController {
The strangest thing happens if I do the same with:
Route::resource('json/stock/equipmentLocations', 'Stock_EquipmentLocationController')
Then I get the message, class not found.
If I move the class from controller/stock/EquipmentLocationController.php to
controller/EquipmentLocationController.php and adjust the route to:
Route::resource('json/stock/equipmentLocations', 'EquipmentLocationController');
Everything works. This happens on a shared server without command line access. On my localhost all controllers can be put into a subfolder.
I wasn't using namespace which worked fine on my local host but gave issues on my public server. Namespacing everything DID solve the issues as suggested by egig. Thanks.
Here's what I did, in case others have similar issues:
I went through this nice tutorial: https://www.youtube.com/watch?v=yAzd7Ig1Wgg
to get familiar with namespacing, as this is relative new for me in for Laravel 4.
router setup (resource router):
Route::resource('json/stock/equipment', 'App\Controllers\Stock\EquipmentController');
Added the psr 0 setting to autoload: (instead of using classmap) like the video suggests.
,
"psr-0": {
"Stock": "app/controllers/"
} ....
Did not add anything to the basecontroller (no namespacing description):
class BaseController extends Controller {
public function __construct(){ ....
Added namespacing and dependencies to my controller in subfolder (controllers\stock):
namespace App\Controllers\Stock;
use BaseController, Form, Input, Redirect, View, Equipment, Response;
class EquipmentController extends BaseController { ....
Then placed my models in a subfolder (models\stock), without namespacing discription.
class Equipment extends \Eloquent {
protected $fillable = [
'description',
'location',
....
Then I did 'php artisan dump-autoload' and uploaded everything to my public server, changed database and public folder settings accordingly, because they are different. And everything worked!
Feedback is welcome, thanks for the support!
I'm working in a Zend Framework based application (Shopware).
I add a template dir in my controller like this:
class Shopware_Controllers_Backend_Pricify extends Shopware_Controllers_Backend_ExtJs
{
public function init()
{
$this->View()->addTemplateDir(dirname(__FILE__) . "/../../Views/backend/");
parent::init();
}
}
But somehow, smarty always looks in the (not existing) part of the controller action:
Unable to load template snippet 'backend/mycontroller/model/main.js' in 'snippet:string:{include file="backend/pricify/model/main.js"} in Smarty/sysplugins/smarty_internal_templatebase.php on line 128
The Controller works over loading via ext js, but I do not see that this is a problem. When I var_dump template directories, the correct dir is included. I debugged the code far into smarty, but never found the part, where the directories are checked.
I'm aware, that this may be a problem within the software stack, but since I do not know where to search, I ask here. If I need to post additional data, please tell me.
I found, that the problem was that shopware extends CamelCase to camel_case folders.
I don't manage to override the skeleton views of the generatorBundle.
I've first tried by adding my view in /app/Resources/SensioGeneratorBundle/skeleton/crud/views/index.html.twig
It didn't worked so I tried to create a new Bundle extending SensioGeneratorBundle and copy the my view in its Resources folder.
I already manage to use themes for twig forms, but I need to personalize the views generated by the doctrine:generate:crud command.
First of all: The corresponding skeleton views are located here:
vendor/bundles/Sensio/Bundle/GeneratorBundle/Resources/skeleton/crud
Quick and dirty you should be fine by overriding these view files - but thats not what we want ;)
In:
vendor/bundles/Sensio/Bundle/GeneratorBundle/Command/GenerateDoctrineCrudCommand.php
there is an accessor for the Generator:
protected function getGenerator()
{
if (null === $this->generator) {
$this->generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
}
return $this->generator;
}
One can try to override this method in your extending Bundle and set a different $skeletonDir in the constructor.
Edit:
Quick example in my test environment how it can be achieved (I only made a quick test ;):
Generate a new bundle for the custom generator: php app/console generate:bundle and follow the instructions. A route is not needed. I chose for this example: Acme/CrudGeneratorBundle (Or use an existing bundle)
Create a folder called "Command" in the newly created bundle directory.
Place a command class in this folder.
<?php
//src/Acme/CrudGeneratorBundle/Command/MyDoctrineCrudCommand.php
namespace Acme\CrudGeneratorBundle\Command;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;
class MyDoctrineCrudCommand extends \Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand
{
protected function configure()
{
parent::configure();
$this->setName('mydoctrine:generate:crud');
}
protected function getGenerator()
{
$generator = new DoctrineCrudGenerator($this->getContainer()->get('filesystem'), __DIR__.'/../Resources/skeleton/crud');
$this->setGenerator($generator);
return parent::getGenerator();
}
}
Copy the vendor/bundles/Sensio/Bundle/GeneratorBundle/Resources/skeleton/crud to your Resources (in my example "src/Acme/CrudGeneratorBundle/Resources/crud")
This was the best solution for me:
symfony2-how-to-override-core-template
doesn't add a command but modifies the skeleton for that particular bundle.
I have this plugin in the plugins directory within the admin module directory. So, it is in application/modules/admin/plugins/LayoutPlugin.php:
<?php
class LayoutPlugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->whatever = 'foo';
}
}
I'd like to use it for sending variables to the layout view. It happens that I get Fatal error: Class 'LayoutPlugin' not found every time I try Zend_Controller_Front::getInstance()->registerPlugin(new LayoutPlugin()); in the admin bootstrap.
How do I load a plugin inside a module?
Module bootstraps setup the module autoloader by default, so if you rename your class to Admin_Plugin_LayoutPlugin ZF should be able to find it.
Keep in mind that the admin bootstrap (like all bootstraps) will be run regardless of whether you're in the admin module or not, so if your intention is to assign some extra variables just for your admin pages you'll need to ensure that admin is the current module before registering the plugin.
I know this is an old question but this is always something that bugged me. I don't know if ZF 2 has done anything to solve it (I've not had the chance to play with it yet), but I wrote a plugin loader plugin to deal with this for ZF 1!
The problem is, of course, that even when setting up a module autoloader and keeping your plugins in a module's plugin folder, this only setups up autoloading (which is cross module anyway) not registration. It means you can instantiate the plugin with a line in your application.ini, but it will be autoloaded and registered for every module.
Anyway, here's a possible solution to make sure module plugins are only registered for the active module. Alternatively, instead of providing a class map, you could loop through all the files in a module's plugins directory, but that feels ugly... and probably slow.
<?php
class BaseTen_Controller_Plugin_ModulePluginLoader extends Zend_Controller_Plugin_Abstract {
private $_pluginMap;
public function __construct(array $pluginMap) {
$this->_pluginMap = $pluginMap;
}
public function routeShutdown(Zend_Controller_Request_Abstract $request) {
$module = $request->getModuleName();
if(isset($this->_pluginMap[$module])) {
$front = Zend_Controller_Front::getInstance();
foreach($this->_pluginMap[$module] as $plugin) {
$front->registerPlugin(new $plugin());
}
}
}
}
Because we need to pass a classMap to the constructor, we need to explicity instantiate and register this plugin with the Front Controller rather than with a line in application.ini:
public function _initPluginLoader() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new BaseTen_Controller_Plugin_ModulePluginLoader(array(
'default' => array(
'Plugin_Foo',
'Plugin_Bar',
...
),
'foo' => array(
'Foo_Plugin_Foo',
'Foo_Plugin_Bar',
...
)
)));
}
The earliest the plugin can run is at routeShutdown otherwise we won't know the active module. What this means though is that any other plugins registered using this method can only run from dispatchLoopStartup onwards. Mostly, we're probably interested in preDispatch and postDispatch hooks but worth bearing in mind.
hi i am a newbie in zend frame work.. pls help mee
i am going through a registration module. and in that module there is separate helper file called Tools for calculating age... date comparison etc
while trying to create object of Users_View_Helper_Tools in indexAction getting a fattal error of Users_View_Helper_Tools not found
how will will we autoload the helper files in the modules
In bootstrap:
Zend_Controller_Action_HelperBroker::addPath(
'Path/To/Where/Your/Helper/Is',
'Path_To_Where_Your_Helper_Is'
);
In controller:
$this->_helper->myHelper();
The helper would look like this:
class Path_To_Where_Your_Helper_Is_MyHelper extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
echo 'Hello';
}
}
To add a helper path, use addHelperPath(); the docs will help you:
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.paths