Cakephp plugin layout is applying to all pages - cakephp

I am facing a strange error in cakephp2.0. I have created two layouts - one for plugin and one for front end users. Whenever I take the front end and after that if I take the plugin in the url, the front end layout is getting applied to all pages coming under plugin and vice verse. After 8-10 refresh it will take the correct layout. How to avoid caching of layout variable in this case ?
VideosController under app/Controller folder
App::uses('AppController', 'Controller');
class VideosController extends AppController
{
public function index()
{
$this->layout = 'default';
$this->set('videos', $this->Video->find('list'));
}
}
My plugin Main controller
class AdminAppController extends AppController
{
var $layout = 'admin';
}
Another controller inside my plugin
App::uses('AdminAppController', 'Admin.Controller');
class VideosController extends AdminAppController
{
public function index()
{
$this->Video->recursive = 0;
$this->set('videos', $this->paginate());
}
}

You can set parts of your view to not be cached, or you could clear the cache every time the layout is changed. Check: http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html

Related

Find() on a non-object?

I am trying to access the find() on another model in a controller. I am getting a
Call to a member function find() on a non-object error
PollsController.php (controller):
class PollsController extends AppController{
public function add(){
$this->set('appNames', $this->App->find('list'));
}
}
Poll.php (model):
class Poll extends AppModel{
public $belongsTo = array ('App');
}
App.php (model):
class App extends AppModel{
public $hasMany = array('Poll');
}
Note: I have also tried $this->Poll->App->find('list') and still get the same error.
App is reserved keyword. When I change the class to SomethingController.php and Something.php, everything works again.
If you want access another model functions you need load model. There is few ways to do it.
This one is only useful for controllers
$this->loadModel('App');
$this->App->find('first');
This one is good for models and controllers
Read more: CakeAPI: Class ClassRegistry
$appModel = ClassRegistry::init('App');
$appModel->find('first');
This one is good for controllers
Read more: CakeAPI: $uses variable in controller
//in Post Controller
public $use=array('Post','App');
Please load modal in your controller
public $uses = array("Poll");

One model to many controllers - CakePHP

Im trying to load one appmodel to many controllers. For example i have appmodel called Item and i want to get some items in HomeController and ContactController. When i execute this code:
class HomeController extends AppController {
public function index() {
$items = $this->Item->find('all');
$this->set('items', $items);
}
}
I got this error:
Call to a member function find() on a non-object
How can i get thing from database in many views in CakePHP?
you need to declare which model you want to use in your controller.
class HomesController extends AppController {
var $uses = array('Item', 'AnotherModel');
...
}
var $uses=array('Item');
If using this model in multiple controllers, put this in app controller.

Relocating Cake's default layouts folder

I'm working on a CakePHP 1.2 app where I don't have control over the /app directory and have relocated my views folder to, say /path/to/mycomponent/views and can render my views by setting my controller's default view path
var $viewPath = "mycomponent/views";
I have some layouts (including basic.ctp) under /path/to/mycomponent/views/layouts
Is there a way to render my views inside a specific layout?
Calling this from my controller gives me a page not found error:
function index()
{
$this->autoLayout = true;
$this->pageTitle = 'Browse items';
$this->render("browse", "basic");
// also tried: $this->render("browse", "/path/to/mycomponent/views/layouts/basic.ctp");
}
Finally got it to work by adding a path to the viewPaths array of the app configuration. This code goes in you controller constructor or in the override beforeFilter metod:
$config = Configure::getInstance();
$config->viewPaths[] = " /path/to/mycomponent/views/";
Now I can just specify the following:
$this->layout = 'basic';
And my controller's views render in the basic.ctp layout.
Just for any further reference.
I believe in cakePhp 2.x it's to be done this way.
public function beforeRender() {
parent::beforeRender();
$this->layout = 'mylayout';
}
which would go to the path: /app/View/Layouts/mylayout.ctp
Note:
If you want to modify the view paths for some controller, add before the controller class declaration:
App::build(array('View' => '/Folder/'));
class ExampleController extends AppController{...}
which will set the path for all rendered view for Example controller to /app/Views/Folder/
so the full example would be:
App::build(array('View' => '/Folder/'));
class ExampleController extends AppController{
public function beforeRender() {
parent::beforeRender();
$this->layout = 'mylayout';
}
public function index() {
$this->render('Home');
//This will load view: "/app/Views/Folder/Home.ctp"
//From layout: "/app/Views/Layouts/mylayout.ctp"
}
}

CakePHP: Find if is mobile browser in a helper (no access to request handler)

I need to know in a helper in a CakePHP application if the device is mobile, I would love to use $this->RequestHandler->isMobile(), but the request handler component is not available in helpers. Any ideas?
Thanks!
You can import the class and use it anywhere in the framework like so:
App::import('Component', 'RequestHandler'); // import class
$requestHandler = new RequestHandlerComponent(); // instantiate class
$isMobile = $requestHandler->isMobile(); // call method
var_dump($isMobile); // output: bool(true) or bool(false)
(Tested from helper and gives correct results for Firefox and iPhone)
Also, any options you set in the Controller::helpers property will be passed to the helper:
class AppController extends Controller {
public $components = array(/*...*/, 'RequestHandler');
public $helpers = array(/*...*/, 'MyHelper');
public function beforeFilter() {
$this->helpers['MyHelper']['mobile'] = $this->RequestHandler->isMobile();
}
}
You can catch the options array in your helper's constructor:
class MyHelper extends AppHelper {
protected $_defaultOptions = array('mobile' => false);
public function __construct($options) {
$this->options = array_merge($this->_defaultOptions, $options);
}
}
The accepted answer suggests using a component inside a helper which should be avoided as components are for use solely in controllers and will result in errors as mentioned by Anupal.
The simple solution is to use the CakeRequest class that RequestHandlerComponent uses. So in your helper you can do:-
App::uses('CakeRequest', 'Utility');
$isMobile = (new CakeRequest())->is('mobile');

Why CakePHP 2.0 is not using my AppController?

I have just upgraded to Cakephp 2.0 alpha, and immediately faced a problem. The app_controller.php in the app/ folder is not loaded in my controllers. Instead controllers use the CakePHP own AppController.php in the lib/Cake/Controller/AppController.php.
I have tried renaming the app_controller to AppController.php aswell but its not working. I have tried deleting the whole file and then copying the lib/Cake/Controller/AppController.php file and then editing it but not working.
I have also tried deleting cache files.
I just simply use:
class AppController extends Controller {
public function beforeFilter() {
die;
}
}
And the application wont die.
My controller is also simply just:
class NewsController extends AppController {
function beforeFilter() {
parent::beforeFilter();
}
function index() {
}
}
So what I could be doing wrong? Im also using Windows 7 + Netbeans with Subversion to update project (if that has anything to do with it :p).
Did much of searching and didn't look from most obvious place: Github cakephp docs: https://github.com/cakephp/docs/blob/master/en/controllers.rst . I thought they were still 1.3 docs but it appears they are updated. Since they are now updated, it says that I should put the old app_controller.php into Controller folder and camel case it to AppController.php.
Create file AppController.php in app/Controller directory
class AppController extends Controller {
public $helpers = array('Html', 'Form', 'Javascript');
public function beforeFilter() {
die('app/Controller/AppController.php file called');
parent::beforeFilter();
}
}

Resources