CakePHP 2.5.2 Themeing and theme.ctp - cakephp

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.

Related

How to preserve layout through different views of same controller in cakephp

I'm using cakephp and I have set a simple site, when rendering index() it works fine
but when programming other methods of the same controller, the views for them do not show the
background, it's like it cannot find the images, I thought the layout would be preserved for all views.
Your View/Layouts/default.ctp layout file will always be used unless specified otherwise. If it's not showing a background, it's like you're using an incorrect path for the image, css...etc.
If you want to apply a layout to all methods of a particular controller (but not all other controllers) then use a theme.
Controller Code:
class MyThingController extends AppController {
public $theme = 'MyTheme';
....
}
Next you have to put your layout file in:
/app/View/Themed/MyTheme/Layouts/default.ctp
Then all methods in your controller will use this layout by default.
See here for more info: enter link description here
(note: this answer applies to Cake version 2.1+)

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.. :)

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

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.

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

cakePHP: I need to include a helper for when there is an error

I have made a helper class called Navigation that gets used on every page because it does stuff to my main navigation menu. So in order for this to work I have included the helper in my pages controller like so:
var $helpers = array('Html', 'Javascript', 'Navigation');
however when there is an error like a missing view or something the helper can't be found and I get a reference to a non-object error that messes my page layout up. I'm guessing this is becuase an error page uses a different controller, however there isn't a file error_controller.php or anything in the controllers file. So my question is where do I need to declare the helper so it can be found by an error page. Would I need to make an error controller file or is there already a file that I can add it in to?
Any help would be much appreciated
Thanks
If it's used on every page, why not add it to the AppController?

Resources