Using $javascript helper issue in CakePHP: "Undefined variable: javascript" - cakephp

I have problem when I want to use $javascript->link('prototype') in the default.ctp layout. It returns:
Undefined variable: javascript [APP\views\layouts\default.ctp, line 6]
I also added this code into app_controller.php:
<?
class AppController extends Controller {
var $Helpers = array('Html','Javascript','Ajax','Form');
}
?>
The file prototype.js is already in webroot/js folder.
Where is the problem?

I have had this problem many times. It's usually either caused by the controller code being overwritten somewhere or some weirdness happening with Cake's automagic stuff. If you remove all of your helpers and then add them one by one it will probably work eventually.
Another perfectly valid way of generating JavaScript links is by using the following which doesn't access the $javascript variable:
echo $html->script(array('prototype'));

It has to be $helpers instead of $Helpers.

You just open the error console of the Firefox browser (shortcut key ctrl+shift+j).
Find the error and click on it.
After clicking, you will see the head portion.
Note the location of the JavaScript file (*.js) which you want to locate (you will see the location is not correct).
Cut the JavaScript file from webroot and paste it in given location of head block.
Example:
This will show on error console. Map_demo is my project, and in its place your project name will display:
<script type="text/javascript" src="/map_demo/js/test.js"></script>
Cut the JavaScript file from webroot
Make the JavaScript folder in your project application folder, /map_demo/js
Paste test.js (your script file) here
Now your JavaScript function will work.

Just in case somebody else comes across this bug/issue: it was also happening to me, until I commented out the line $session->flash(); in my default layout. Realising that the error was being caused by flash messages, I went back to the controller and noticed that I was using separate layouts for flash messages (e.g. 'message_alert') and that those layouts didn't actually exist in the view folder!
So remember, errors like this could mean that a file is not defined yet. Best of luck.

Related

Add javascript file at the end of the script block

i am using cakephp 3.7 and i load few .js file in my default.ctp layout. The problems comes when i try to add other .js in my view. These js files are added at the beginning of the js file list.
I do echo $this->fetch('script'); to print that block.
For example, i am using jquery, i load this library in default.ctp because i use it everywhere, the problem is that view js are loaded before jquery so i cannot use $
how can i "append" to script block, inside an action's view?
Thanks
In your layout put (before < /head> if you wish)
$this->fetch('my_head_script')
, then in your view use
$this->Html->script("jquery.js",['block' => 'my_head_script']);
Did You use script blocks, described here: https://book.cakephp.org/3.0/en/views/helpers/html.html#creating-inline-javascript-blocks?

CakePHP 2.5.2 Themeing and theme.ctp

I've created a simple application that is about to be vastly expanded upon and get a bit out of hand if I don't get the theme sorted out early as I need to start working with tinyMCE.
I created my theme in app/View/Themed/Default and added the following into my app controller
public $theme = 'Default';
Something seems to be working because cakePHP is not rendering my views and is giving me the following errors.
Error: The view for {controller name}Controller::add() was not found.
Error: Confirm you have created the file:
/home/cake/public_html/app/View/Themed/Default/{controller}/theme.ctp
I've been reading through the documentation and nowhere in there does it tell me that I need to create a folder for every controller and add a theme.ctp file inside. so I'm at a bit of a loss as to what needs to go into this theme.ctp file.
If I add the Controller folder, and a blank them.ctp file, the view is not rendered. So I am assuming something vital is required here.
I discovered the problem.
Further down the page from legacy code that was ported across, the following code was sitting at the bottom of the page.
AppController.php
function beforeRender() {
$this->view = "Theme";
$this->theme = "default";
}
So it was being forced into rendering a view that did not exist.

cakephp v2 layout not loading

Hi I want to add a new layout to my cakephp, but somehow the system keeps looking in the view folder from the controller instead of the /app/View/Layouts folder.
Error: The view for TestsController::desktop() was not found.
Error: Confirm you have created the file: /app/View/Tests/desktop.ctp
the desktop.ctp file is in /app/View/Layouts. The same place as the default.ctp
The code in the controller is:
public function desktop() {
$this->layout = 'desktop';
}
What is wrong here? I don't understand why cakephp keeps looking in the view/controller-name folder... I need this fixed because I want to use this layout for other controllers. Thanks.
If you read the message carefully, you'll see cake is telling you it cannot find your view, not your layout.
So, create an empty /app/View/Tests/desktop.ctp and see what happens. I'm hoping magic.. :)

Experiencing strange behaviour with cakephp

I was setting up this application in cakephp.I have homes controller which works well and then i have chokates controller which has an index action.But whenever i run this chokates controller i get an error
Call to a member function charset() on a non-object
I had a print of $this in top of this controller which shows me that html and javascript helpers are loaded already.Then why i get this error i dont know.
Please help
Here is the link http://www.maninactionscript.com/chokate
go to the chocolates link.
Regards
Himanshu Sharma
Well, looking at your error message it appears that a couple things are happening here that should be fixed:
The view for ChokateController::index() was not found.
Ensure that you have an index.ctp file in /app/views/chokate/.
The HtmlHelper object likely isn't being added to the view because you have a manually configured list of $helpers. If you have assigned an array to $helpers in ChokateController ensure that Html is listed.

Simple examples on how to use CakePHP pages_controller.php to create static pages

I am complete beginner to CakePHP but I am a bit knowledgeable in ROR.
Can somebody pls give me some simple examples on how to make use of pages_controller.php? I want to create static pages such as Home, About, and etc but I don't know how and where to start. I tried something like creating a about.ctp in the views and creating about_controller.php (this is how being done in Ruby on Rails) but I just got some errors.
I concluded that all of the static pages will only use 1 controller which is pages_controller.php but I dont know how.
I tried reading the article found on this link:
http://book.cakephp.org/view/958/The-Pages-Controller
but it doesn't give me anything that will help me learn how to use it.
This is what I got from the page:
CakePHP core ships with a default controller called the Pages Controller (cake/libs/controller/pages_controller.php). The home page you see after installation is generated using this controller. It is generally used to serve static pages. Eg. If you make a view file app/views/pages/about_us.ctp you can access it using url http://example.com/pages/about_us
When you "bake" an app using CakePHP's console utility the pages controller is copied to your app/controllers/ folder and you can modify it to your needs if required. Or you could just copy the pages_controller.php from core to your app.
Can somebody pls show me or explain to me how??? I am a total beginner pls help.
It's pretty self explanatory.
Create a file in your APP/views/pages/ folder - e.g about_us.ctp
Type in your content. No layout; just the text, tables/images/etc
<h3>About my site</h3>
<p>bla bla la</p>
<?php echo $this->Html->image('my_img.jpg'); ?>
Save.
Go to www.site.com/pages/about_us - your page is served.
Pages is the controller to serve static pages. you don't need an about_controller, unless you need something more than just a static page.
You can change how the link looks by using routing.
You can set variables for use in your template as well:
about_us.ctp
<?php
$this->set('title_for_layout', 'My about page');
$this->set('active_link', 'about');
?>
<h1>My page!</h1>
etc

Resources