Why CakePHP 2.0 is not using my AppController? - cakephp-2.0

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();
}
}

Related

Force CakePHP plugin to use its own layout

I have written a CakePHP plugin https://github.com/anuj9196/CakePHP-App-Installer
The plugin is using default.ctp layout from plugin_path/src/template/layout/default.ctp
When there is some other theme used in the host application. Like in my case I have setup one in AppController's beforeRender()
$this->viewBuilder()->setTheme('DashboardTemplate');
DashboardTemplate is in application's /plugin/ directory.
Now, when I access my plugin's URL using example.com/installer/install
The template loads on top of DashboardTemplate theme.
How can I disable them in plugin's AppController?
The AppController inside plugin directory contains
<?php
namespace Installer\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
// nothing here
}
Remove the theme by using beforeRender() in your plugin's AppController.
<?php
namespace Installer\Controller;
use App\Controller\AppController as BaseController;
class AppController extends BaseController
{
/**
* #param \Cake\Event\Event $event The beforeRender event.
* #return \Cake\Http\Response|null|void
*/
public function beforeRender(Event $event)
{
try {
return parent::beforeRender($event);
} finally {
$this->viewBuilder()->setTheme(null);
}
}
}
You can switch between layouts in your view and controllers fairly easily.. using plugin syntax
// inside controller
$this ->layout = 'Plugin.layout';
//inside view template
$this ->layout = 'Plugin.layout';
If you just want to disable the theme, use Mathew's method above. But be careful that will disable the theme for the whole application not just this plugin in case some of your app code is ran after your plugin

CakePHP change locale dynamically

I've my CakePHP app internationalized through .po files.
The file structure is as follows:
- src
- Locale
- en_EN
- en_ES
The app is correctly translated at startup by:
ini_set('intl.default_locale', 'en_ES');
But, I need the app to be translated dynamically, in an action listener button for example.
I tried the following, but it doesn't work:
use Cake\I18n\I18n;
I18n::locale('en_EN');
You need to save the locale in the session so that it persists between page requests.
A possible approach:
class AppController extends Controller {
public function initialize() {
if ($this->request->session()->check('Config.locale')) {
I18n::locale($this->request->session()->read('Config.locale'));
}
//rest of your init code
}
public function change_locale($locale){
$this->request->session()->write('Config.locale', $locale);
return $this->redirect($this->referer());
}
}

Cakephp plugin layout is applying to all pages

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

CakePhp ACL permissions

Checking my application, I saw that every user can access to all the actions in it.
I'm using cakePhp build-in ACL Component...
Checking permissions through terminal displays correctly is the user is allowed or not to call a certain action. But once I'm checking the application on the browser all users have access to every action. Any clue what could be doing this?
You can have CakePHP automatically handle things for you if you're using the built-in Auth and ACL components. To start, you can make sure you have an app_controller.php file in the App folder. Mine looks something like this:
<?php
class AppController extends Controller {
var $helpers = array('Form', 'Html', 'Javascript', 'Time');
var $components = array( 'Acl', 'Auth', 'Session', 'Cookie');
function beforeFilter() {
$this->Auth->authorize = 'actions';
$this->Auth->actionPath = 'controllers/';
$this->Auth->authError = ' Access Denied!';
$this->Auth->loginRedirect = '/registrations';
$this->__checkAuth();
}
private function __checkAuth() {
$currentUser = $this->Auth->user();
$currentUser = $currentUser['User'];
$this->set(compact('currentUser'));
}
}
?>
If you're authorizing 'actions' then try including that code in your app_controller.php file, or create one if you don't already have one. Then start browsing to see if it has made any changes.
If you have custom code in each controller's beforeFilter, you'll also need to add a single line of code to each controller.
function beforeFilter(){
parent::beforeFilter();
}
Any beforeFilter (even a blank one) placed in a controller will override the beforeFilter of the AppController unless you specifically call the AppController's beforeFilter using the code above.
You can also find some of the best tutorials on using CakePHP's ACL here: http://aranworld.com/article/161/cakephp-acl-tutorial-what-is-it

How can I set the title_for_layout in the default PagesController?

I cannot set title_for_layout in the PagesController that comes by default with CakePHP 1.3.
I am using the following code in the display function:
$this->set('title_for_layout','some title');
What am I doing wrong?
In your controller, the corresponding value is $this->pageTitle.
UPDATE
Oops, as noted in the comments, this is the 1.2 solution. 1.3 possibilities (after doing some research) include:
Ensuring that $title_for_layout is being echoed in the layout
Placing the $this->set() code in the view rather than in the controller
If you'd like to mimic the behavior of cake 1.2, you can do the following:
In your app_controller, create the following method:
in app/app_controller.php (you may need to create this file if you haven't already)
public $pageTitle;
public function beforeRender() {
$this->set('title_for_layout', $this->pageTitle);
}
Then in any of your action methods, you may then use the pageTitle as you would in 1.2.
public function index() {
$this->pageTitle = 'Some Title';
}
The beforeRender() method will be called after your controllers have finished processing, but prior to the layout being rendered, thus allowing you to set variables for the layout.
In the action method, try this:
function index()
{
$this->pageTitle= 'Your Title';
}
Just thought I'd add to any new people finding this solution, you can do $this->set('title', $title); in CakePHP 1.3 inside the controller and the title will be rendered automatically.
You can use :
$this->assign('title', 'Some title');
and in ctp :
<title><?= $this->fetch('title'); ?></title>
It work in CakePHP 3.0
For CakePHP 3.x,
you can follow the advice here
Essentially,
Inside UsersController.php:
$this->set('title', 'Login');
Inside src/Template/Layouts/default.ctp
above the $this->fetch('title');
write:
if (isset($title)) {
$this->assign('title', $title);
}
Use beforeRender() instead. Put the following in AppController:
class AppController extends Controller {
var $content;
function beforeRender() {
$this->set('content',$this->content);
}
}
And in the controller, you can just do:
function index() {
$this->content['title'] = '123'; }
That'll do the trick.

Resources