How to get resource in controller action?
Resource db was initialized in application.ini.
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// I want db resource here
}
}
Try and see if this works:
$this->getFrontController()->getParam('bootstrap')->getResource('db')
UPDATE : While this solution works, it is NOT a recommended
practice. Please, read comment by
#Brian M. below.
You can use Zend_Registry. Initialize the database connection in the bootstrap and store it in the registry:
// set up the database handler
// (...)
Zend_Registry::set('dbh', $dbh);
Then you can retireve it from anywhere else:
$dbh = Zend_Registry::get('dbh');
In answer to a similar question on Nabble, Matthew Weier O'Phinney (Mr Zend Framework 1) suggests using this form:
$this->getInvokeArg('bootstrap')->getResource('db');
So, in the context of this question, it would be something like:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// db resource here
$db = $this->getInvokeArg('bootstrap')->getResource('db');
}
}
Related
I am new with Symfony 2 and Doctrine.
I have created a new class on my symfony project, which is located in:
project_folder/src/Libraries/Validarcontenido/Validarcontenido.php
I need to get the Doctrine Entity Manager instance in this class.
I have reading docs about it, and everyone says that i must add the class into the services.yml file, but didn't work.
Here is my code:
<?php
namespace Libraries\Validarcontenido;
use AdminBundle\Entity\Aportes;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
class Validarcontenido
{
private $request, $post, $em;
public function __construct()
{
$this->request = Request::createFromGlobals();
$this->post = $this->request->request->all();
// gets doctrine instance
$this->em = $this->getContainer()->get('doctrine');
}
}
And the services.yml:
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/book/service_container.html
parameters:
# parameter_name: value
services:
validarcontenido.service:
class: Libraries\Validarcontenido\Validarcontenido
arguments: [#doctrine.orm.entity_manager]
But Symfony returns me this error message:
Attempted to call an undefined method named "getContainer" of class "Libraries\Validarcontenido\Validarcontenido".
500 Internal Server Error - UndefinedMethodException
What i am doing wrong? Thank you so much.
You have to use the constructor to set the entity manager:
class Validarcontenido
{
private $request, $post, $em;
public function __construct(EntityManager $em)
{
$this->request = Request::createFromGlobals();
$this->post = $this->request->request->all();
$this->em = $em;
}
}
You need to create your class as a service in the service.yml as you already did, only you were almost there.
You need to change your class as this:
<?php
namespace Libraries\Validarcontenido;
use AdminBundle\Entity\Aportes;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Request;
class Validarcontenido
{
private $request, $post, $em;
public function __construct(EntityManager $em)
{
$this->request = Request::createFromGlobals();
$this->post = $this->request->request->all();
// gets doctrine instance
$this->em = $em;
}
}
Heres a working example (that provides the container): https://codedump.io/share/tpxgpEMJnaiW
Ok, the problem was that when i create the service instance in the controller i used this:
$my_class = new My_class();
And now i know that i have to use this:
$my_class = $this->get('service_name');
Thank you so much!
i have problem in zend framework basic application that class is not found. please help
i have basic structure and added model/tables directory. After my login page is displayed when i submit it then following error occurs
Fatal error: Class 'Application_models_tables_User' not found in E:\zendu\Apache2\htdocs\login\application\controllers\AuthController.php on line 18
Authcontroller.php
class AuthController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
// action body
}
public function loginAction()
{
$data = new Application_models_tables_User();
}
models/tables/User.php
class Application_models_tables_User extends Zend_Db_Table
{
protected $_name = 'users';
}
in application.ini
appnamespace = "Application"
i am using zend studio so basic structure is automaticaly created. so i have not to set include path of models.
Have a read through this :
The module resource autoloader sets the folder 'models' to the namespace Model.
I would change your class name to Application_Model_Table_User, and change your 'model/tables' folder to models/Table
According to this blog post it should be possible to have an init() method within the model. Like this:
class MyModel
{
public function init()
{
// prepare something
}
...
}
This does not work for me. Does anybody know if I have to set somewhere a parameter (e.g. in the config file) or if this is not possible any more in my Zend FW version (1.11.4)? I checked with error_log('something') within the init method and it does not get called at all.
UPDATE:
I have extended Adam's solution a little:
public function __construct() {
if (get_parent_class($this) != false) {
parent::__construct();
}
if(method_exists($this, 'init')) {
$this->init();
}
}
This way it can be placed in a base class and then extended. Just in case someone needs the same solution later too.
That post is very misleading. If the model classes in the application extend Zend_Db_Table or Zend_Db_Table_Row, which is not the case in the post's examples but is implied from the text, then yes you can add an init() method that will be called automatically. For this to work in your application you would need to be calling the method from whatever creates your models, or from the constructor of a base class.
Try adding a call to init() from the constructor as so:
public function __construct()
{
parent::__construct();
$this->init();
}
you have that problem because a php class does not have an init method. That init method is only applicable to Zend classes. Which mean you can only use it when you extend a Zend class. Which means you are actually overriding the init method.
E.g. here it is in the Zend_Db_Table_Abstract class:
/**
* Initialize object
*
* Called from {#link __construct()} as final step of object instantiation.
*
* #return void
*/
public function init()
{
}
So if you change your model to extend zend it will work:
class MyModel extends Zend_Db_Table_Abstract
{
public function init()
{
// prepare something
}
...
}
It's the same with Zend_Controller_Action, which is why you can also use it in your controllers.
In this example I want increase the session variable called "test" by one each time it enter to the controller. If comment the content of the method preDispath works fine but with this precise example, the session variable "test" increase in 3 or 5 each time.
I use Zend Framework 1.11.4
Why??? I hope you understand my question.
Remember this example is only to show the strange behavior of the method preDispatch
My plugin
class App_Plugins_Permisos extends Zend_Controller_Plugin_Abstract{
public function __construct(){}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$request->setModuleName('default');
$request->setControllerName('index');
$request->setActionName('index');
}
}
My bootstrap
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
protected function _initSession(){
Zend_Session::start();
}
protected function _initPlugins(){
$this->bootstrap('frontcontroller');
$this->frontController->registerPlugin(new App_Plugins_Permisos());
}
}
My Controller
class IndexController extends Zend_Controller_Action{
public function init(){}
public function indexAction(){
$s = new Zend_Session_Namespace('test');
if(isset($s->test)){
$s->test++;
}else{
$s->test = 1;
}
Zend_Debug::Dump($s->test);
die();
}
}
Thanks a lot
Try it putting in .ini resources.frontController.plugins.foo = "My_Plugin_Foo"
If works tell me! Tnks
Yall:
I have a simple question, it might be a simple configuration issue, but
I have a Model defined, and when I try to access it from a controller it
fails.
The Model is in the model directory, and when I look at the quickstart app,
it seems like this should work.
Here is my model:
<?php
class Application_Model_User {
protected $_user;
protected $_password;
protected $_userId; // very simple right
}
?>
My controller just stops.. here is the controller code:
<?php
class UserController extends Zend_Controller_Action {
public function init() {
}
public function indexAction() {
// display login form
$users = new Application_Model_User();
echo "test never echos.. stopped above ? weird huh.."; // fails before ..
}
?>
Thank you everyone,
In application.ini
appnamespace = "Application_"
App structure:
/application/
/models/
/User.php
Class definition:
class Application_Model_User {}
Should work OK.