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.
Related
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().
i am working on a Cakephp 2.3 ..In my modals i am doing encryption an decryption in these two functions beforeSave and afterFind .. as again and again i have to write this
Security::rijndael($text, Configure::read('constants.crypt_key'), 'encrypt');
so i decided to make a function so i have done this
static public function encrypt($text) {
return Security::rijndael($text, Configure::read('constants.crypt_key'), 'encrypt');
}
static public function decrypt($text) {
return Security::rijndael($text), Configure::read('constants.crypt_key'), 'decrypt');
}
but i want to know where should i write these function.. should it be in app/lib/utility or app/vendors directory and also after suggesting, do tell me how can i access the function in the Model ..how can i import the class in Model..thanks in advance
To use a common function on controller side you have to declare it in 'AppController.php'
While to use function in view files you can mention it in 'AppHelper.php' And for model you can put it in 'Appmodel.php'
It depends where you want to be calling them from. If you're only calling them from your model (which I think makes sense in your case), then you should place them in AppModel.php, which all your models inherit from.
however, having seen your previous question, if you're having to write the encrypt/decrypt function "again and again", then you're probably not designing your app very well.
Really, you should only need to call encrypt once, in your beforeSave, and decrypt once, in your afterFind. If you have to call them in one or two other places... OK. But if you're having to call them all over the place, you're going about things the wrong way.
And also, there should be no need to make it a static function.
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
Is it possible to access the current request in Kohana's bootstrap? I tried accessing Request::$current but $current doesn't seem to be defined at that stage. Is there any way around that? Also at what point in the application is Request::$current defined?
It's not possible, because Request object is created in index.php after including bootstrap.php:
// Bootstrap the application
require APPPATH.'bootstrap'.EXT;
/**
* Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
* If no source is specified, the URI will be automatically detected.
*/
$request = Request::factory();
If you must access it, do it in the index.php after it has been created, although maybe you could tell us what exactly are you trying to do?
You can use it after Kohana initialization.
Kohana::init(...);
Also, good practice is using interface methods instead of public variable. I'm wondering why the developers keep $current as a public field.
So.. use
Request::current();
Also, It seems that using
Request::initial();
is better idea. But it depends on your realization.
I have an admin controller that I would like to utilize functions in other controllers (these functions do not represent pages that someone would load in their browser), but it cannot utilize those functions because the functions in the other controllers are private. They are private because I don't want the public to access them. Is there a way to make a controller function not accesible to the public without making the function private or protected?
public function __blah(){
// function that can't be accessed from outside, but can be called from other functions
}
Based on what I've read in the comment of the answer Piotr gave you:
You don't use an admin controller. You want to use admin prefixes:
http://book.cakephp.org/view/950/Prefix-Routing
And authentication:
http://book.cakephp.org/view/1250/Authentication
If you call - and thats how your comment sounds like - one controller from another you're doing something totally wrong in an MVC framework. If it should be re-usable code it should go into components if it's about admin action use the prefix routing and admin_* methods, auth component and protected methods for what you call "helper" methods.
Yes.
You have a lot of information in the CakePHP Book about ACL (access control list) and that is exactly what you're looking for.
Or you may use Auth component.
I see three possible solutions (they can also be combined):
The first solution is to move the code you want to reuse to components (as mentioned by burzum).
The second solution depends on your code. It's possible that you do stuff in the controller which should be done in the model. In this case, move the respective code to the model.
The third solution is to put the code you want to reuse into plain old PHP classes and load them as vendor files.