How can i call simple custom php function in zend controller ?
class Ozone_IndexController extends Zend_Controller_Action
{
public function snetworkAction(){
}
function curl_file_get_contents($url){
}
}
how i can call curl_file_get_contents($url) function inside snetworkAction
Your question is definitely ZF2-agnostic. It really boils down to OOP basics.
Have you set up your route for snetworkAction? If so, then the contents of your action, to call that other object method, would simply be:
public function snetworkAction(){
$this->curl_file_get_contents( 'http://someurl.com' );
}
private function curl_file_get_contents( $url ){
...
}
I would strongly recommend getting a grasp on basics before going deep into ZF2. It's really going to be a frustrating experience if you don't have the fundamentals down pat. It's a very unique framework, and while excellent, will cause neophytes to bleed!
Good luck man.
you can call curl_file_get_contents($url) like
$this->curl_file_get_contents($url);
it is better to define function as protected
Related
Is it possible to mock super class method call? I have seen many posts, but they are either irrelevant or using different testing framework.
Is it possible with easymock?
If not, what other framework would allow me to do it?
No it's not. And I don't think it is with other frameworks. That would required bytecode manipulation of the base class. So maybe Powermock but I'm not sure.
However, I have never needed to do that in 20 years. In general, it means a bad implementation of the template pattern.
So instead of something like
public void foo() {
// do stuff
super.foo(); // don't forget to call super
// do some other stuff
}
you better do
base class:
public void foo() {
doBeforeFoo();
// ... stuff that is in super
doAfterFoo();
}
and then you fill the holes in the child class
It has been a rough task to learn this framework. I am still stuck at quick start.
I am using PDO_MYSQL as my adapter. I have followed the tutorial to the letters.
At a point, it says to create application/models/GuestbookMapper.php and put the following code
class Application_Model_GuestbookMapper
{
public function save($model);
public function find($id, $model);
public function fetchAll();
}
Then it say to run this command
zf create model GuestbookMapper
The tutorials claims to insert additional methods. But in my case, it emptied the my GuestbookMapper class with this
class Application_Model_GuestbookMapper
{
}
Same goes with another command zf create model Guestbook. It empties the scripts, with just plain class defination.
Please help me !
Re-read the QuickStart carefully. You're probably skimming and getting hung up.
Here is the text:
A typical API for a data mapper is as follows:
class Application_Model_GuestbookMapper
{
public function save($model);
public function find($id, $model);
public function fetchAll();
}
And then...
In addition to these methods, we'll add methods for setting and retrieving the Table Data Gateway. To create the initial class, use the zf CLI tool:
% zf create model GuestbookMapper
Lastly...
Now, edit the class Application_Model_GuestbookMapper found in application/models/GuestbookMapper.php to read as follows:
Note that the first example is just that - an example. They are providing a sample interface of what your class will probably look like. The second block of text explains how to create your initial (empty) class. And the last block gives you the exact final code of the GuestbookMapper class.
Just add those methods again ;) Won't that work?
Sometimes I have complicated find procedures and I'm feeling dirty to repeat this code in my Controller.
Now I am thinking, it is possible to do something like this:
class User extends BaseUser
{
private static function getTable()
{
return Doctrine_Core::getTable('User');
}
public static function findAll()
{
return getTable()->findAll();
}
public function currentEnrolments() {
$query = Doctrine_Query::create()
->from('Enrolment e')
->where('e.user_id = ?', $this->id)
->addWhere('e.finish_date IS NULL');
return $query->execute();
}
}
Is this a good practice? Or should I only put non static members like the query I have shown?
Generally, if it saves you time, there nothing to lose and every minute you can save to gain.
Functions like getTable and findAll are probably not saving you a lot, but custom queries for finding stuff more specific to your application will definitely be worth it.
I have got pretty much the same approach.
I wouldn't bother with your static proxies of getTable() and findAll().
That doesn't really add any value to your code
I, personnaly, never call findAll() on any model object as you will generally need
to cross-check against a foreign key
paginate / sort
...
regarding your currentEnrolments() function, this is worth doing as you have a bit of logic on this ->addWhere('e.finish_date IS NULL'), thus explaining you can't use the "magic" Doctrine relation ->Enrolment. Maybe this is something Doctrine 2 is resolving, need to check this out ...
Regards
As mentioned in the Zend Framework manual, I created a base controller.
Subclassing the Action Controller
By design, Zend_Controller_Action must
be subclassed in order to create an
action controller. At the minimum, you
will need to define action methods
that the controller may call.
Besides creating useful functionality
for your web applications, you may
also find that you're repeating much
of the same setup or utility methods
in your various controllers; if so,
creating a common base controller
class that extends
Zend_Controller_Action could solve
such redundancy.
But it turns out, that Exceptions are not being called properly from the base... To replicate this, create a file:
/path/to/workspace/library/Joe/Controller.php
Then:
class Joe_Controller extends Zend_Controller_Action
{
public function init()
{
Throw new Zend_Exception('test', 500);
parent::init();
}
}
Then in your controller directory, IndexController.php extends off the base:
class IndexController extends Joe_Controller
{
You will find that the exception is uncaught.
If however, you don't extend off the base controller, then throw an exception in init or preDispatch, it will be caught and forwarded to the ErrorController.
Anyone have an idea on getting the exceptions caught from the Base Controller?
Thanks.
UPDATING TO TACKLE THIS ANOTHER WAY
After looking at the various articles and official documentation on how to structure the directories, I went with putting the base controller in library/Joe/...but maybe that's part of the problem...there's no guidance in the manual on how to name and place the base controller. What do the experts with a base controller do?
Another Update
Looking at my code some more, and reading on the net, seems like people suggested in should be:
abstract class Joe_Controller_Action extends Zend_Controller_Action.
Except changing it did not solve the problem...
Now considering that there are articles suggesting to use Base Controllers including the manual, would this be considered a bug in Zend Framework?
To replicate, just throw a Zend_Exception of any kind in the base init or preDispatch. Imagine you poll the database in there, (which is what I'm doing)...and the database is down. No error controller. That's not a good situation unless I'm doing something incorrectly.
My suspicion is that this is a new bug... I don't recall this problem before Zend_Application and I've been using a base controller since ZF 1.5.
Two thoughts off the top of my head:
Controller names in the default module are usually named like SomethingController. Your name Joe_Controller suggests the module Joe and an empty controller name.
In routing/dispatch, doesn't it look for a matching action before dispatch to the controller? Since the samples have no actions, might you be triggering ControllerNotFound exception?
Just throwin' out some ideas.
in Zend philosophy, they provide the init() method to avoid the hassle of
public class Module_TotoController extends Zend_Action_Controller {
public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
parent::__construct($request, $response, invokeArgs);
// some init code here
}
}
just use :
public class Module_TotoController extends Zend_Action_Controller {
public function init ()
// some init code here
}
}
Now, If you look at the Zend_Controller_Action class source, you will notice that the following methods are empty:
init()
preDispatch ()
postDispatch ()
This means, it is useless to call parent:: in your specific controller
public class Module_TotoController extends Zend_Action_Controller {
public function init ()
parent::init(); // useless ;-) but you can go for it
// some init code here
}
}
Now, if you want to put an intermediate class between Module_TotoController and Zend_Action_Controller, you expose yourself to a big hassle as:
Some controllers won't extend your "base" controller (e.g your ErrorController in your question)
so it is not a really a base of your application, is it ?
If you put some logic in your init() of that "base",
you must call the parent:: in your init(),
all the developers in the project need to be aware of that
You will ever add another little features to your "Base" controller
This will result to a big bloated file
Loading/Initializing plenty of stuff you might not really need in that lambda controller
Do you need you database on every page ?
No: doesn't a $this->_helper->connect(); look nice instead ?
Yes: use a controller plugin
This "Base" controller won't fit your other projects needs, so that class won't be reusable
Action helpers will
Controller Plugins will
Hope it makes sense
Don't go for a base or whatever controller
As documentation says :
By design, Zend_Controller_Action must be subclassed in order to create an action controller.
Agreed, this is misleading
What they meant is
class Module_TotoController extends Zend_Controller_Action {}
class Module_TotoController extends Joe_Controller is plain wrong (sorry no offence)
as you said your ErrorController can't extend your intermediate Joe_Controller class
because your exception will be thrown again in the instantiation of the ErrorController class (as excepted !!!)
The uncaught Exception error is the result of a protection to avoid loops (dig Zend\Controller\Plugin\ErrorHanlder.php line 200-ish)
If you require something to be done for every action, use a Zend_Controller_Plugin and register it with the frontController
im making an application and i need to implement diferent level of user permits.
I coul have the function
$this->view->users->hasPermits($this->view->user);
By declaring a function on the model, an things could be easy to implement. But i would like to be able to have the next function doing the same:
$this->view->user->hasPermits();
Is there a way to do this? do i need to extend the Zend_Db_Table_Row class? any ideas?
Use Zend_Acl for this.
There is a Zend ACL tutorial on YouTube.
If $user is a Zend_Db_Table_Row from the Zend_Db_Table $users, than in User you could perform the method like so:
public function hasPermits()
{
return $this->getTable()->hasPermits($this);
}