How to enable Auto Complete in PHPStorm for CakePHP templates.
For example $this and its functions, Helpers ... etc in the *.ctp files
Just add a type hint comment on top of your view code:
<?php
/* #var $this \App\View\AppView */
For the records, in pre-namespaced CakePHP/2.x it used to be:
<?php
/* #var $this View */
Related
I am using AdminLTE theme now i want to add a functionality in header in which i need data from database how can i get this done in cake php 3 . I have done it by calling query and getting data in view but that is violation of mvc. How can i get data from database and use this data in every view.
I think You can use Cake's Controller event beforeRender() inside AppController. Using this functionality, You can simply output data for every method in controllers which extends AppController. I use it quite often for this kind of functionalities.
<?php
// src\Controller\AppController.php
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
class AppController extends Controller
{
/**
* Before render callback.
*
* #param \Cake\Event\Event $event The beforeRender event.
* #return \Cake\Network\Response|null|void
*/
public function beforeRender(Event $event) {
$this->loadModel('Notifications');
$notifications = $this->Notifications->find('all' , [
'conditions' => [
// get notifications for current user or default demo notification
'user_id' => $this->Auth ? $this->Auth->user('id') : 0
]
])
->toArray();
$this->set(compact('notifications'));
}
}
Then in your templates You have access to $notifications variable
<div class="list-group">
<?php if(count($notifications)):foreach($notifications as $item): ?>
<div class="list-group-item">
<?= h($item->content) ?>
</div>
<?php endforeach;endif; ?>
</div>
To make it more common you can make an element of header and footer. And please don't ever call query and get data in view. You can get data in your controller's respective method and set data in view(also in element).
For how to create element please visit https://book.cakephp.org/3.0/en/views.html#using-view-blocks.
And I recommend to complete this tutorial to get more in-depth idea of CakePHP https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/installation.html
Please comment if you like to know more or have any query.
I wish to add a version number to my css file but I don't see a way to do it with the html helper.
I see one example of adding a timestamp through the bootstrap.php but I want to be able to control the version number myself.
I am using cakephp 2.3
style.min.css?v=1
<?php
echo $this->Html->css('style.min', array('v'=>'1'));
echo $this->fetch('css');
?>
or
<?php
echo $this->Html->css('style.min?v=1');
echo $this->fetch('css');
?>
Just add the .css
echo $this->Html->css('style.min.css?v=1');
Although it's standard practice to omit the .css, it's acceptable to include it, and will allow you to add additional parameters to the end of the URL.
In CakePHP 3x you can do this in config/app.php.
/**
* Apply timestamps with the last modified time to static assets (js, css, images).
* Will append a querystring parameter containing the time the file was modified.
* This is useful for busting browser caches.
*
* Set to true to apply timestamps when debug is true. Set to 'force' to always
* enable timestamping regardless of debug value.
*/
'Asset' => [
// 'timestamp' => true,
],
I use PhpStorm 6.0.2 and CakePHP 2.3.
In my controller file I define this and get autocomplete for my custom components:
/**
* #property MysuperComponent $Mysuper
*/
Regarding to this, in my view files I define this to reach Cake's core helpers and this works:
/**
* #var $this View
*/
I need autocomplete for custom helpers inside my views. I tried this but didn't work:
/**
* #property Myelegant $MyelegantHelper
*/
When I do this, this works partially:
/**
* #var $Myelegant MyelegantHelper
*/
I get this $Myelegant-> autocomplete. But it isn't adequate. I need autocomplete like this: $this->Myelegant->
Notes: Autocomplete successfully works for core helpers inside view (ctp) files. But not for custom helpers.
Add new file /app/View/HintView.php
Add your custom helpers' names on PHPDoc.
<?php
App::uses('View', 'View');
/**
* #property MyelegantHelper $Myelegant
* */
class HintView extends View {
}
Inside your layout files or View files (ctp files) add this code on top
/**
* #var $this HintView
*/
Now inside your views you can see like this:
$this->MyElegant
->Blocks
->Cache
->Form
$this->MyElegant->somefunction()
anotherfunction()
oldfunction()
You don't have to extend your Views from HintView. It is only for PhpStorm's autocomplete.
(Note that you can make it faster with creating shortcuts to codes. For example goto Settins / IDE Settings / Live Templates. Add new template. For example "myeleg" for "$this->MyElegant->" So when you write "myeleg" and press Tab key it will write the class name automatically)
Have you tried looking at this article:
http://blog.hwarf.com/2011/08/configure-phpstorm-to-auto-complete.html
Look at the section "Setting Up Helper Auto-completion in Views". Hopefully this helps.
I know this is an old post, but came across this having the same issue. Solved it this way:
For me, my custom helper is called StatusHelper, so needed the #property as follows:
App::uses('Helper', 'View');
/**
* #property StatusHelper $Status
* */
class StatusHelper extends AppHelper {
Then in the view .ctp file, I just needed the following at the top:
<?php /* #var $this View|StatusHelper */ ?>
Now the autocomplete works for my PHPstorm in that view for both core View vars as well as whatever ever methods are in my helper.. Happy days
Using Cake 2.5 - PHPstorm 10. Hope this helps someone else out there...
Is Easy, Test in CakePHP 3.x to PHPStrom, supports namespace.
Add in to file views.ctp type PHPDoc
<?php /** #var $this Cake\View\View */ ?>
I have used this item and get this error :
Missing Helper
Error: JavascriptHelper could not be found.
Error: Create the class JavascriptHelper below in file: app/View/Helper/JavascriptHelper.php
<?php
class JavascriptHelper extends AppHelper {
}
Well indeed, this file does not exists, and I tried to use 'Js' in my helper array.
class myClassController expend AppController {
var $helpers = array('Html', 'Js'); // and not 'Javascript');
In the code, the method $this->Javascript->codeBlock is called to add a javascript method (in the middle of the content instead of the header) but there is no $this->Js->codeBlockcodeBlock either.
$output .= $this->Js->codeBlock("datepick('" . $htmlAttributes['id'] . "','01/01/" . $options['minYear'] . "','31/12/" . $options['maxYear'] . "');");
Could you explain me what happened to the old Javascript helper or how to get the code working?
Is there an other helper which could work with CakePHP-2.0?
Cordially,
Have you read the migration guide? If not do that now:
http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html#xmlhelper-ajaxhelper-and-javascripthelper-removed
XmlHelper, AjaxHelper and JavascriptHelper removed The AjaxHelper and
JavascriptHelper have been removed as they were deprecated in version
1.3. The XmlHelper was removed, as it was made obsolete and redundant with the improvements to Xml. The Xml class should be used to replace
previous usage of XmlHelper.
The AjaxHelper, and JavascriptHelper are replaced with the JsHelper
and HtmlHelper.
JsHelper JsBaseEngineHelper is now abstract, you will need to
implement all the methods that previously generated errors.
So
$this->Js->codeBlock('...');
is now
$this->Html->codeBlock('...');
HtmlHelper::scriptBlock($code, $options = array())
//Parameters:
$code (string) – The code to go in the script tag.
$options (array) – An array of html attributes.
I followed the tutorial to create a blog for CakePHP 1.3 up to this step but keep getting error when running the app:
Notice (8): Undefined property: View::$Html [APP\views\posts\index.ctp, line 27]
Line 27:
echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id']));
Apparently CakePHP doesn't load the HtmlHelper class, I check over and over again in my controller, the Html should be loaded properly.
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html', 'Form');
function index() {
$this->set('posts', $this->Post->find('all'));
}
}
When I added this line to the view (index.ctp), it works
$this->Html = &$this->loaded['html'];
But apparently I can't do that for every ctp file. I'm running Windows 7, WAMP 2, PHP 5.3.5, CakePHP 1.3.7 stable.
Anyone has a clue?
Like the comment by mtnorthrop above:
Is the FormHelper being loaded in your
views? What do you get if you do
pr($this->Html) in your view? How
about pr($html)? Until CakePHP 1.3,
helpers were accessed directly instead
of through the View object. In CakePHP
1.3 both methods should work. Does the plain $html->link() or $form->input()
methods work for you? – mtnorthrop 51
mins ago
From the book:
"The HtmlHelper is available in all
views by default. If you're getting an
error informing you that it isn't
there, it's usually due to its name
being missing from a manually
configured $helpers controller
variable."
You shouldn't need to specify it in your controllers. Perhaps somehow doing so is interfering with the core? BTW, you don't need to specify Form either.