How to preserve layout through different views of same controller in cakephp - 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+)

Related

CakePHP 2.x How to determine view path in controller in beforeRender() or earlier?

I have the need to determine what app path my controller method will use for serving up the view before it does so. I'm using a theme but I also have many non-themed view files. I'm switching my themes based on domain name (2 domains point to the same Cake install) but need to exclude the non-themed views from rendering inside my theme.
This may sound confusing. Here's what is currently happening if a URL is accessed that does not have a theme view associated with it:
domainA.com/examples/index will render the view app/View/Examples/index.ctp with the layout from app/View/Layouts
domainB.com/examples/index will render the view app/View/Examples/index.ctp BUT with the layout from app/View/Themed/MyTheme/Layouts
This is because the "MyTheme" theme does not contain a view file for this controller-method pair (this is intentional). So I would like to instead have the following established:
domainB.com/examples/index continues to render the view app/View/Examples/index.ctp BUT INSTEAD with the layout from app/View/Layouts
This should only happen, of course, if and only if there is no view file within the "MyTheme" directory structure.
I think this is what you are looking for $this->View->viewPath.
You can use this in any controller call back function or action.

Creating static home page for CakePHP3

I am working on a CakePHP3 project. I want a static homepage that will be loaded on www.mysite.com.
For this I have created a PagesController which will handle all the static pages in the website like about,contact,etc.
I am having display.ctp view in Template/Pages/display.ctp to load on www.mysite.com.
But, for testing (routes are not configured yet), I'm using www.mysite.com/pages and www.mysite.com/pages/display to show the view but it gives error as
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.pages' doesn't exist
Do I need to create Table for this ?
It is much, much easier than that
For this I have created a PagesController
There is already a pages controller for serving static content, and a
static template for the home page, there is no need to create/overwrite the default pages controller, to replace it with the same (or less) functionality. This is also mentioned in the documentation
The steps to modify static pages (with default routes) are:
edit src/Template/Pages/home.ctp - look at the url /
create/edit src/Template/Pages/something.ctp - look at the url /pages/something
The error means that the application is looking for a model named Page. To tell the application that your controller does not refer to any model you have to use something like bellow. Also add the proper action. www.mysite.com/pages/display means in controller "pages" call action "display".
class MyController extends AppController {
var $uses = false;
public function display {}
}

Include utility class all over the application cakephp 3

I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass.
I can include the Utility class in controller .e.g.
use App\Utility\ArrayUtil;
and then call the class in my controllers e.g.
ArrayUtil::myMethod();
And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well.
I tried to load it in config/bootstrap.php but I get this error:
Error: Class 'ArrayUtil' not found
Any idea
You can add this line at the top of the page, be it Model, view or controller.
use App\Utility\ArrayUtil;
If you're using this utility in multiple views, then I'd suggest you to write this line in Template/Layout/default.ctp, since all templates would be a part of this.
Hope this helps.
Peace! xD

Customizing layouts with CakePHP based on the current record

Does anyone have recommendations on how to customize the layout sitewide based not on the current view, but the data associated with the view? In most cases, the models we are using have an associated Club id, so would have to customize the layout header image, css, etc, depending on which Club the current page is associated with.
Here is what I am thinking so far.
Call a function in the model associated with the current controller, which gives you the layout parameters.
In appController, beforeRender, set layout parameters for the club.
This doesn't seem very elegant, because each model would have to have this function, and how would I call the right model if beforeRender is defined in the app controller?
Some tips would be great!
Russel,
you might want to look at this.
Simply create a file in layouts called "dashboard.ctp" then if your view is index, your function would be something like this...
function index() {
$this->User->recursive = 2;
$this->layout = 'dashboard';
$this->set('users', $this->paginate());
}
The important line being $this->layout = 'dashboard';
What we ended up doing is checking in the app controller for the domain the site is currently being loaded from, then setting the layout based on this information (have a layout for each domain the site needs to look customized for). Not a great solution but it works fine.

How to load custom plugins in CakePHP?

I'm writing a poll plugin for a website based on CakePHP. The plugin works good if I access it from its own URL (eg. myapp.com/plugin/controller) but I need to call it from different pages. I would like to include it as a widget in every page.
I'm looking for a method like $myplugin->renderPoll($pollId); but I really didn't find any information about how to instantiate the Polls class. I tried with App::import and ClassRegistry::init with no luck.
Any suggestion ?
Thank you
Looks like you are trying to create some sort of Helper to create poll cross views? I would suggest creating a Helper for that particular class. Simply create a helper in plugins/plugin_name/views/helpers/foo.php, and in each controller (or in app_controller.php) that you need it, include the helpers as $helpers = array("PluginName.Foo"); and inside your view, you should be able to use the methods defined in foo.php by calling $foo->renderPoll($pollId).
//app/plugins/plugin_name/views/helpers/foo.php
class FooHelper extends AppHelper {
var $name = "Foo";
function renderPoll($id=0) {
//...
}
}
Use Elements! They're small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout.
Check this link out: http://book.cakephp.org/view/1081/Elements
I guess this link explains everything you need.

Resources