Extend RedisEngine in CakePHP - cakephp

I have some custom methods created in Cake's RedisEngine file to do things like Redis spop...etc. I know that editing the actual RedisEngine file itself within the Cake Lib is not ideal, but am unsure exactly how to extend it so that I can add my own methods. Or more specifically, if I do extend it, how to I tell Cake to use MyRedisEngine instead of the default one?

Per this page in the CakePHP Book, you can extend CacheEngine by specifying the config like this in Config/bootstrap.php:
Cache::config('custom', array(
'engine' => 'MyCustomCacheEngine', // if this doesn't work, try without the 'Engine'
// ...
));
and adding a MyCustomCacheEngine.php file in the app/Lib/Cache/Engine/ directory.

Related

Creating a view (cakePHP noob)

I've been reading until my eyes are swollen, and having troubles finding what should be a simple answer.
I am not new to PHP, but I am new to cakePHP. Please bear with me, and have patience with my ignorance of lack of knowledge of the terminology.
I have been asked to help make some fixes on a cakePHP developed site, that was recently created.
The site is missing a page for "http://domain.com/logout". I can see the functions I need to access in the UserController, but I am not sure where to put the .ctp file to generate the view.
Let's just say I want the logout.ctl file to be something as simple as:
echo "Hello World";
Under my app/View folder I have the sub-folders Home, & User that I have tried to place this file into. I am assuming I have to do something else as well, but I have not been able to locate what that is.
Any assistance is appreciated. Thanks for reading!
1.By default, you should link your view and controller together by creating Views/Controller/action.ctp.
Since the url is linked to Controller by routes, views are not associated with it directly.
For example, if you have set
Router::connect('/logout/', array('controller' => 'User', 'action' => 'logout'));
, then you may want to create Views/User/logout.ctp.
If you have set
Router::connect('/logout/', array('controller' => 'Home', 'action' => 'logout'));
, then you may want to create Views/Home/logout.ctp.
2.You can change the view in your action with $this->view='sample' or $this->render('sample'); and then create the view file with name sample.ctp.
3.You can also read a view of another folder with $this->render('/Sample/logout');.
Reference: http://book.cakephp.org/2.0/en/controllers.html
4.If you use themes $this->theme = 'Example';, the default view file will be set to /app/View/Themed/Example/Posts/edit.ctp.
Reference: http://book.cakephp.org/2.0/en/views/themes.html
5.I think the default extension of cakephp view files is .ctp but not .ctl. .ctl is used by Microsoft Visual Studio? I'm not quite sure.

I don't get chosen-cakephp it working in simple application

I am new to CakePHP, MVC and web developing in general (JQuery, etc), altough I have more than 15 years programming non web applications and some little knowledge of PHP.
In order to learn, I am developing a simple school project containing 3 tables (courses, professors, courses_professors).
I have "baked all" and got a nice CRUD application which works perfectly.
Now I am trying to improve select boxes userfriendliness provided by harvesthq/chosen by using Chosen-CakePHP.
I have followed the instructions for installation and setup of the plugin at https://github.com/paulredmond/chosen-cakephp, but I am not sure about what more steps should I follow in order to make it work. Btw. I understand creating a custom class is optional.
To be true, I expected that modifing /app/Controller/AppController.php as explained in would be enough to see the new select boxes working, but the application works just as it did before adding the plugin.
The code contained in /app/Controller/AppController.php is the following (comments removed):
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Chosen.Chosen');
}
app/Config/bootstrap.php contains:
./...
CakePlugin::load('DebugKit');
CakePlugin::load('Chosen');
./...
I have been googling for 2 days, but I don't get the idea.
Do I need to modify any code generated by bake? Do I need to modify anyother files?
I guess my lack of knowledge in the field is quite relevant. Should you reccomend me an essential previous reading or exercise, any reccomendation is welcome.
Any indications would be appreciated.
Thanks for your help,
ivan
I have never work with the plugin you mention, but look at the examples in the link you provided
(in the view)
echo $this->Chosen->select(
'Article.category_id',
array(1 => 'Category 1', 2 => 'Category 2'),
array('data-placeholder' => 'Pick categories...', 'multiple' => true)
);
The helper used is not $this->Form->input, as it comes with baking, it's $this->Chosen->select. So my guess is "yes, you need to modify the code generated by bake".
Try that, and if it doesn't work, update your question with the code you tried for the view and if it gives you any (new) error.
You don't really need a plugin to get such a small functionality for your forms.
First you need to download a Chosen package.
Open your webroot folder and put there all dependent js, css files that comes with package
Open default layout file app/Views/Layouts/default.php (if you "baked all" that should be default layout file)
Read about inserting js, and css files into your layout here Html Helper
Syntax is simple:
This code:
echo $this->Html->css('forms');
Outputs:
<link rel="stylesheet" type="text/css" href="/css/forms.css" />
Similar for js files..
Add this code to layout footer
$(".chzn-select").chosen();
or header (but then you need to wrap it with document.ready ;
In your baked view files add class .chzn-select to select form elements
echo $this->Form->input('Model.field', array(
'type' => 'select',
'multiple' => true,
'class' => 'chzn-select'
));
If you just started learning, it's to early to just "bake all"

CakePHP default layout for xlsx

I have a method which exports riport data into an xlsx file in my CakePHP 2 project. It is working well. In the same time it seems it just ignores anything what I put into View/Layouts/xlsx/default.ctp
In my routes.php I have
Router::parseExtensions('json', 'xlsx');
In my controller I have
public $components = array('RequestHandler');
My View/Riports/xlsx/export.ctp is rendered, but View/Layouts/xlsx/default.ctp is ignored.
What do I miss?
I think you're out of luck here. From the documentation;
The data view classes don’t support layouts. They assume that the view file will output the serialized content.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files
But I may be wrong of course :)

CakePHP - Default labels to placeholders instead

So, in CakePHP, there is a way to turn off labels by default by putting 'label'=>false in 'inputDefaults' in the Form->create() statement.
Is there a way to, instead of removing labels altogether, to change labels into element placeholder attributes? This is equivalent to doing the 'inputDefaults'=>array('label'=>false) on the Form->create while adding a 'placeholder'=>'Placeholder' to each element... but is there a way to do it without having to add it to each element and have CakePHP do it for you?
You could run your own copy of FormHelper that contains your modifications. You can copy the version of FormHelper from lib/Cake/View/Helper/FormHelper.php and paste it into your app at app/View/Helper/FormHelper.php and make the required modifications to your copy. The copy in your app folder takes precedence over the one provided in the core. As always, never ever ever make modifications to files in Core.
Alternatively, you could extend FormHelper into your own Helper class:
class MyFormHelper extends FormHelper {
...
}
Don't forget to add 'MyForm' into your $helpers array!
At any rate, what you're asking is not currently possible using Cake 2.x. Without any changes, you'll need to add a 'placeholder' => '...' in the options array of each input() call. If you make this change, please consider contributing it back to the CakePHP community so others can benefit from your work!

How to do form-based file uploads in CakePHP?

I have been looking into this for a while and can't figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!
Firstly your form needs to be set up to allow file uploads.
<?php echo $form->create(Model, array('type' => 'file')); ?>
This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).
Once the file has been uploaded you should handle everything else from within the Model:
function beforeSave($created) {
extract($this->data[Model][field]);
if ($size && !$error) {
move_uploaded_file($tmp_name, destination);
$this->data[Model][field] = destination;
}
return true;
}
These are only the basics, so be sure to have a play around to find the solution that best fits your needs.
You can use Zend Components to handle the file upload. There is a good example here on my website:
CakePHP file upload using Zend Components
NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.
In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.
You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.

Resources