Replacement for AppModel in Cakephp3 - cakephp

I would like to implement an autoslug-function for two models in CakePHP3 and use the beforeSave-callback for that which works fine. However the slug structure is a bit different than in Inflector::slug, so I wrote a small function for the different structure which finally leads to my question
In Cake2 I would have placed this helper function in AppModel which is not existing anymore. What's the best way to do that now? A behaviour (which seems a bit biggish for 2 lines of code) or class AppTable extends Table or ..?

Make it a behavior and use it where needed instead of putting it in a super model class. If you put that into a plugin and repository you can simply add your plugin as package via composer for every app that needs this plugin.

Related

Override Cakephp 3 core class

I'd like to override a function that exists in the CakePHP 3 core (to be more specific Cake\Database\ValueBinder).
How can I achieve this? I tried copying the class to src/Database/ and changed the namespace from Cake to App. I use'd the class in AppController, but had no luck.
Thanks in advance
Adrian
ndm helped me a lot with this (see comments to my question), but since he didn't post an answer I am doing this now.
It's possible to use an own ValueBinder by setting it on my query: $this->Table->find()->valueBinder(myOwnValueBinder).
To make that the default ValueBinder in my own code (sufficient in most cases) one can use e.g. an AppTable as known from CakePHP 2 that extends to Table and make all other *Table classes extend AppTable. Now one only needs to create the following method:
public function query() {
return parent::query()->valueBinder(myOwnValueBinder);
}
In case it's needed that all code uses teh custom ValueBinder, an extended datasource Connection class has to be created (and used). In this class Connection::newQuery() has to be overriden.

cakephp - how to define cake php helpes correctly

I have an ItemsController which extends AppController.
To handle my site menu, I used a menuhelper.
However, I saw in the documentation that you have to add your helpers in an array in your controller (AppController):
public $helpers = array('Form', 'Html', 'Menu');
The weird thing is that I forgot it and my menu worked anyway.
Also, you always have to add Form and Html extra to the array because otherwise they don't work anymore.
However, when I do something like this in my AppController, my form helpers still work:
public $helpers = array('Menu');
So it seems that whatever I do, it still works, but I don't get why and I don't like automatic "magic" :)
Is there something I am missing in the docs?
Since 2.x you don't "need" to specify app or core helpers.
Those are lazyloaded automatically.
You only need to specify plugin helpers manually.
That said I personally still always describe what helpers I use, just to be consistent with the plugin ones.

Best configuration and parameters for ctags in a CakePHP project

What is the best configuration and parameters for ctags in a CakePHP project?
I want to be able to auto-complete ctp files, Components, Behaviours, Models and Helpers?
Check these github repositories, I have found then and they are so good for work with php and cakephp
https://github.com/amix/vimrc
https://github.com/ndreynolds/vim-cakephp
This solution requires 1 line in your .ctags file and two lines in your .vimrc file, so it's fairly minimal.
tl;dr
.ctags:
--langmap=php:+.ctp
.vimrc:
# Controller -> Component
map <leader>t yiw<cr>:tag /^<C-R>"<CR>
# View -> Helper
map <leader>h yiw<cr>:tag /^<C-R>"Helper<CR>
Add Views to your tags
This solution is mostly for jumping between files. I'll try and add auto-completion at a later date.
Add this to your ~/.ctags options file to include CakePHP views as PHP files:
--langmap=php:+.ctp
Then I'm assuming you've done ctags -R . at the root of your project (that's what I've done at least). This out of the box should pick up PHP syntax and class definitions.
Auto-completion (general)
I found the auto-completion (omni-completion from Ctrl+XCtrl+O) doesn't work very nicely with PHP, e.g. if I type $this-> and then try to auto-complete it doesn't find any tags.
The fix for this was to use install phpcomplete.vim. This will find methods within your class.
However that won't auto-complete connected models.
Models
By default ctags should work for all Controller -> Model jumping as the Model name is the same as the class name.
Behaviors
These again should be fine as you don't specify the name of the behavior you just have the method name which depending on how independent the name is it should get found - or at least it will be in the list of tags.
Components
There's no direct way of mapping these, I couldn't see a way of mapping them through the ctags --regex options. ctags recognises that they are classes but doesn't know the xxx -> xxxComponent mapping.
However there is one slight trick. You can do a tag search on the beginning of the class name (source)
:tag /^Email
will find
class EmailComponent
You can then map this in your .vimrc
map <leader>t yiw<cr>:tag /^<C-R>"<CR>
This copies the word that you've got the cursor over and then pastes it into the tag command and executes it. My leader is set to ,, so I can type ,t and it takes me to the corresponding component under the cursor.
Helpers
Ok, another slight hack in the .vimrc file:
map <leader>h yiw<cr>:tag /^<C-R>"Helper<CR>
Using ,h, this will jump you from $html->... to
class HtmlHelper extends AppHelper {
But it doesn't work for functions inside e.g. if your cursor is over script in $html->script, it will not take you to the HtmlHelper script method. So it's a work in progress.

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!

CakePHP custom function (but not global)

I have a module that handles translations.
It is not bound to any database, the file is purely and simply something like this:
$arr["key1"]="text";
...
I need to make Ajax calls in order to edit that file. That file will be imported each time a page is accessed in order to deliver any text content that I need (it may not be the best thing of the world but it does the job, and it's supposed to be really fast since there's no "XML parsing" kind of thing, it is simply stored in a standard .php file).
To handle these ajax calls that will have actions like Add/Edit operations, I have made a TranslationController that is Model-less:
class TranslationsController extends AppController {
public $uses = array(); // Model-less
public $components = array('RequestHandler');
public $helpers = array('Session');
protected $translationFilePath;
public function setItem() {
}
public function backupFile() {
}
}
My problem is simple: where do I put custom functions that are used to open/write/find in files?
For example I have a function that extracts a key from the line that is being read.
I've seen some posts where we are advised to place the data inside the model but in my case I have no model AND it's these functions are not linked to the data. So it shouldn't be in the model, right? I do not want it to be global.
Thank you for your help.
Why are you not using the built in translation stuff that comes with CakePHP and is based on the more or less standard gettext tools?
What you're doing is just re-inventing the wheel and probably coming up with a non tested customized translation implementation that is obviously lacking feature that CakePHP alreay offers you. For example how do you handle plurals?
CakePHP offers you __(), __d(), __n(), __dn() for translations and the translation files are stored in APP/Locale/ as plain text files and are edited with poedit.
See http://www.gnu.org/software/gettext/ and http://book.cakephp.org/2.0/en/core-libraries/internationalization-and-localization.html
Sounds like a use case for a custom datasource: http://book.cakephp.org/2.0/en/models/datasources.html.
Personally, I would just create a normal PHP class in the app/Lib folder of your application.

Resources