CakePHP cakeError()? - cakephp

I can't find any information of where cakeError() is defined as member-function. The documentation only states that the call looks like this:
$this->cakeError(string $errorType [, array $parameters]);
But calling this in my AppController subclass gives me Call to undefined method EntriesController::cakeError().
Where is cakeError() defined?

CakePHP 2.x:
http://book.cakephp.org/2.0/en/development/errors.html
For 2.0 Object::cakeError() has been removed. Instead it has been
replaced with a number of exceptions. All of the core classes that
previously called cakeError are now throwing exceptions. This lets you
either choose to handle the errors in your application code, or let
the built in exception handling deal with them.
CakePHP 1.3:
http://api13.cakephp.org/view_source/object/#line-187
http://api13.cakephp.org/class/object#method-ObjectcakeError

Related

Sencha Touch 2.3.x - is there a universal method to call a function from another class?

I'm stuck with a very simple issue - calling functions between classes. Say I have a function (renderMap) in one of my already defined class: App.ux.MyClass (I also added this class to 'requires' in app.js). How to call the 'renderMap' function from other classes?
I tried App.ux.MyClass.renderMap() but I got 'undefined is not a function'.
I would solve the problem by creating a mixin that would contain all functions shared by many classes. See the mixins docs for details.
Then you would just call
this.renderMap()
in any class that uses that mixin.
I think it's problem in application architecture.
You can to use DI (it will be best choice), but, if you cannot, try to create Singleton or ServiceLocator patterns (yes, I know they are anti-patterns).
In ExtJS 4.x and Sencha Touch 2.x Singleton can be created via statics definition in class. Read more: http://docs-origin.sencha.com/touch/2.3.2/#!/api/Ext.Class-cfg-statics
Then, you just can to call method like App.ux.MyClass.methodName().

Can I call Cakephp validation via static methods?

I want to use the built in validations in cakephp 2. Just like password hashing:
AuthComponent::password()
I have tried:
Validation::email($email)
But that gives a fatal error (class not found etc.). Is there a quick way/hack of using it in a static way?
If the method is declared static (public static function) - which it is in your case - you can do so.
But you still need to tell Cake where to look for.
So if you havent used/included the class yet, you need
App::uses('Validation', 'Utility');
prior to calling any of its methods.

Controller naming convention

I have an existential question that I hope someone can answer.
Why is it that Cakephp controller needs to have the "controller" word attached?
I know what is the naming convention, but nowhere does it says why is it so (well, maybe it's written somewhere, but I haven't found it).
Controller classnames are plural, CamelCased, and end in Controller
We don't have PostModel.php, WhateverModel.php or viewView.ctp, addView.ctp. With models it's just Post.php or Item.php. With views is... well, anything, but there's no need of a "View" append at the end of the action. Why the difference with controllers?
I have seen a few questions where the error is that they have ItemModel.php, and I understand why the confusion if the controller is ItemsController.php (though, granted, it's not an excuse to not read the docs).
I thought maybe it was to help avoiding inflection problems, like with a Fish.php model, the controller would be also Fish.php if we didn't add the controller part. But the separation of folders is quite clear and having
/Controller
Fish.php
/Model
Fish.php
isn't really a problem... or is it?
My question is why, not how the naming convention for controllers is like that, and if there's any logical reason besides a "just because". Even a "we started like that in version 0.0.1 and then it was to late to change it" would suffice.
Models are the only classes that do not have the type appended.
Something <- model class
SomethingBehavior <- behavior class
SomethingHelper <- helper class
SomethingController <- controller class
SomethingComponent <- Component class
SomethingView <- View class
You can not do the following:
/Controller
Fish.php
/Model
Fish.php
Ever tried importing two classes into PHP with the same class name? (CakePHP expects the class name to match the file name since 2.x)
Fatal error: class `Whatever` already exists (or something similar)
Before PHP 5.3 and namespaces this is what had to be done to avoid these fatal errors. Since CakePHP 2.x and below was targeted at versions of PHP below 5.3 and namespaces this was what was done.
Also to make importing classes easier file names map to class names, this is why you can not have Fish.php with class FishController. That would avoid the Fatal error for sure, but Cake does not load classes like that.
Even though CakePHP 3.x will target PHP 5.4, the Controller/Component/Behavior etc will continue to exist for backwards compatibility (probably, its still there in the latest 3.x branch)

cakephp function calling

whats the use of calling a cakephp function
http://localhost/project/controller/method.hello
so whats hello?
but it requires views/method/hello/method.ctp...
It seems there are some errors in your description.A typical CAKEPHP url should something like:
webroot/controller/method/argument
and a method must have a view file.See typical request of cakephp
As SpawnCxy noticed, maybe you did not understand well the request "philosophy".
Anyway, using routes you can define your own, but the default is
/:controller:/:function:[/:arguments:]
remember that you can also use Named Parameters

Is using the RequestHandlerComponent in a model possible?

I'm new to PHP and decided to use the cakePHP framework to help me get started.
I can't figure out one thing though, I want to call methods on the RequestHandlerComponent class to update a users last used IP address and other information, I figured the best place to put this would be in the beforeSave() method on the User model.
I can't figure out how to call the getClientIP method though.
The normal code that would otherwise go in the controller doesn't work. Is there another way to call this class if you're in the model and not the controller?
Class Level:
var $components = array('RequestHandler');
And in the function:
$this->data['User']['lastActiveIP'] = $this->RequestHandler->getClientIP();
Gives:
Undefined property: User::$RequestHandler
Call to a member function getClientIP() on a non-object
Components, by design, aren't available to models (without bypassing MVC convention - which you can do, of course). If you chose to force it to be available, look into ClassRegistry::init(). A better solution, I think, would be to employ the RequestHandler component in your controller (where it's meant to be used), set the lastActiveIp value in the controller (exactly as you've shown in your own example code) and pass the entire data array along to the model.
Now your component is being used where it should be and the model gets to remain ignorant about where it gets its data. At the risk of oversimplification, all the model should know is what to do with the data once it arrives; let the controller worry about collecting and packaging the data.
In addition to Rob's answer, maybe it's enough to put a bit of code together yourself that uses the general env('REMOTE_ADDR') or similar variables. Look at the RequestHandler code, it's not doing anything terrifically complicated.
You may even be able to call the component statically, which is slightly better than instantiating it in the model (still in violation of MVC though). Untested, but should work:
App::import('Component', 'RequestHandler');
RequestHandlerComponent::getClientIp();

Resources