zend framework can't find Model classes? - zend-framework

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.

Related

Is there a way to use MongoDB without ORM in Laravel 5?

Basically, I see Eloquent (for that matter, any ORM) as overhead, as MongoDB itself deals with document objects.
I am looking to use native PHP MongoDB code with application wide database connection object, for a greater performance.
Any library or a simple way to achieve this?
I have read a few things and used PHP MongoDB driver with custom "Model" code, with base class like below:
AppModel.php
<?php
namespace App;
use MongoClient;
use MongoId;
use Log;
class AppModel {
public $collection;
public function __construct() {
$mongo = new MongoClient();
$model_name = (new \ReflectionClass($this))->getShortName();
$collection_name = str_plural(strtolower($model_name));
$this->collection = $mongo->selectCollection('proj_zabbit', $collection_name);
}
public function findById($id) {
return $this->collection->findOne(array(
'_id' => new MongoId($id)
));
}
// more wrapper functions ..
}
Extended model class:
<?php
namespace App;
class Message extends AppModel {
}
In Controller:
<?php namespace App\Http\Controllers;
use App\Message;
class MessagesController extends Controller {
public function __construct()
{
$this->Message = new Message;
}
public function get()
{
$id = Input::get('id');
$message = $this->Message->findById($id);
return $message;
}
}

zendframework action helper to display form

I want to have a login form when i click www.example.com/login with the help of action helper .
My controllers/helpers directory contain the file name
the class Login.php is
In my views/login/index.phtml i m calling it like this
<?= $this->loginForm; ?>
Login.php
<?
class Zend_Controller_Action_Helper_Login extends
Zend_Controller_Action_Helper_Abstract
{
function Login($a)
{
//Consists of form elements
}
}
?>
I get the following error ---------
"Helper "login" does not support overloading via direct()"
How to display the form and what is the error about ?
I was able to get it right this way .
http://geekabyte.blogspot.nl/2012/08/understanding-zend-frameworks-plugin.html
in the Bootstrap.php
public function _initLoader()
{
//For configuring Zend Application to be aware of the Controller Action Helper
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers');
}
The LoginController.php
class LoginController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->_helper->login->scream();
}
}
In the controller/helpers/Login.php
class Zend_Controller_Action_Helper_Login extends Zend_Controller_Action_Helper_Abstract
{
Public function scream()
{
echo "Here please help!!!";
}
}

zend framework: class not found

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

Strange behavior of the Controller_Plugin

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

How to get resource in controller action?

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');
}
}