CakePHP: Redirect not workin - cakephp

I have the following code in my controller:
class ContactsController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('contacts', $this->Contact->find('all'));
}
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid contact'));
}
$contact = $this->Contact->findById($id);
if (!$contact) {
throw new NotFoundException(__('Invalid contact'));
}
$this->set('contact', $contact);
}
public function add() {
if ($this->request->is('post')) {
$this->Contact->create();
if ($this->Contact->save($this->request->data)) {
$this->Session->setFlash('Your contact has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your contact.');
}
}
}
}
In the add() method I have the line $this->redirect(array('action' => 'index')); I'm expecting this line to redirect back to my index page within my view. But all I get is a blank white page.
Any help appreciated.
Regards,
Stephen

Your code looks valid; I don't see an error in the code you provided.
There are a few possible causes;
there is an error somewhere else in your code, but cake php doesn't output the error message because debugging is disabled. Enable debugging by setting debugging to 1 or 2 inside app/Config/core.php - Configure::write('debug', 2);
you application is outputting something to the browser before the redirect header was sent. This may be caused by (non visible) white-space before or after any <?php or ?> . This will cause a 'headers already sent' warning, and the browser will not redirect. There are many questions regarding this situation here on StackOverflow, for example: How to fix "Headers already sent" error in PHP
Note;
in CakePHP its good practice to put a return before any redirect statement; this will allow better Unit-testing of your application, i.e.:
return $this->redirect(array('action' => 'index'));
update; additional 'pointers' on locating the cause
Tracking these kind of problems may be troublesome, I'll add some pointers
If you're using the Auth or Security component, it's possible that one of these cause the 'problem' (e.g. authorization failed or the posted data is marked 'invalid', which will be handled by a 'blackHole()' callback. Disable both in your AppController to check if the problem is still present
If the problem is still present, it is possible that headers are being sent (as mentioned before), but no warning/error is presented. To find if and where those headers are sent, add this code to your 'add' action;
Debugging code:
if (headers_sent($file, $line)) {
exit("headers were already sent in file: {$file}, line number: {$line}");
}

After lots of research finally I got a solution for that.
Please use
ob_start(); in AppController.php
ob_start();
class AppController extends Controller {
function beforeFilter() {
parent::beforeFilter();
}
}

Related

CakePHP Controller Testing: Mocking $this->referer()

I have code in place so that if the "Add User" page is accessed from anywhere in the "Posts" section of the website, the user will be taken to the "Users" index after adding the user. But if the "Add User" page is accessed from any other section of the website, the user will be taken back to where they were after adding the user. I want to test this, but I don't know how. This is what I have so far:
Controller Code
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect($this->request->data['User']['redirect']);
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
else {
if ($this->referer() == '/' || strpos($this->referer(), '/posts') !== false) {
$this->request->data['User']['redirect'] = Router::url(array('action' => 'index'));
}
else {
$this->request->data['User']['redirect'] = $this->referer();
}
}
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
}
Test Code
<?php
App::uses('UsersController', 'Controller');
class UsersControllerTest extends ControllerTestCase {
public function testAdd() {
$this->Controller = $this->generate('Users');
// The following line is my failed attempt at making $this->referer()
// always return "/posts".
$this->Controller->expects($this->any())->method('referer')->will($this->returnValue('/posts'));
$this->testAction('/users/add/', array('method' => 'get'));
$this->assertEquals('/users', $this->Controller->request->data['User']['redirect']);
}
}
What am I doing wrong?
You aren't mocking any methods
This line
$this->Controller = $this->generate('Users');
Only generates a test controller, you aren't specifying any methods to mock. To specify that some controller methods need to be mocked refer to the documentation:
$Posts = $this->generate('Users', array(
'methods' => array(
'referer'
),
...
));
The expectation is never triggered
Before asking this question, you probably had an internal conversation a bit like: "why is it saying that my expectation is never called? I'll just use $this->any() and ignore it.."
Don't use $this->any() unless it really doesn't matter if the mocked method is called at all. Looking at the controller code, you're expecting it to be called exactly once - so instead use $this->once():
public function testAdd() {
...
$this->Controller
->expects($this->once()) # <-
->method('referer')
->will($this->returnValue('/posts'));
...
}
The full list of available matchers is available in PHPUnit's documentation.

CakePHP unit test code coverage $this->request->data clause not executed

I am completely new to PHP unit testing (using PHPUnit) and CakePHP(2) as a framework, and I'm coming back to PHP after 5 years away.
I've got a website up and running and am writing unit tests as I go along as best practice. However, xdebug is showing that one of my clauses is not covered when I believe I am calling it and I just can't see why. I've googled the hell out of all search terms I can think of and re-read the relevant sections of the cookbook and (while I've learned a lot of other useful things) I didn't find an answer so am hoping that a simple answer is forthcoming from someone in the know :)
Here are the relevant sections of code:
Controller:
<?php
App::uses('AppController', 'Controller');
// app/Controller/ClientsController.php
class ClientsController extends AppController {
/* other functions */
public function edit($id = null) {
if (!$id) {
$this->Session->setFlash(__('Unable to find client to edit'));
return $this->redirect(array('action'=>'index'));
}
$client = $this->Client->findById($id);
if(!$client) {
$this->Session->setFlash(__('Unable to find client to edit'));
return $this->redirect(array('action'=>'index'));
}
if ($this->request->is('post')) {
$this->Client->id = $id;
if ($this->Client->saveAll($this->request->data)) {
$this->Session->setFlash(__('Client has been updated.'));
return $this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('Unable to update client'));
}
}
if (!$this->request->data) {
$this->request->data = $client;
$this->Session->setFlash(__('Loading data'));
}
}
}
Test:
<?php
// Test cases for client controller module
class ClientsControllerTest extends ControllerTestCase {
public $fixtures = array('app.client');
/* other tests */
public function testEdit() {
// Expect success (render)
$result = $this->testAction('/Clients/edit/1');
debug($result);
}
}
?>
The code executes as expected. If I browse to "/Clients/edit/1", the flash message (Loading data) I expect is displayed indicating that there was no request data, so it's loaded from the $client. The correct data displays in the edit form.
When I call from within my test, I get a success message that the test has passed but xdebug code coverage is showing the if (!$this->request->data) { .. } clause is not covered, and no errors are apparent.
This seems counter-intuitive to me, so in a hope to avoid frustration with future (more complex) unit tests - can anyone explain why the test would pass but not execute this clause when it is called during normal access of the page?
(The fixture is correct both in terms of data structure and inserting the data before I'm attempting to edit it. Calling edit() from a test case with no id or an invalid id correctly executes the relevant clauses, as does passing data that does not pass validation.)
I've got a similar problem and I solved by adding the second parameter to testAction():
$this->testAction('/Clients/edit/1', array('method' => 'get'));
Also you may want to change your
if ($this->request->is('post') {
...
}
if (!$this->request->data) {
...
}
To:
if ($this->request->is('post') {
...
} else {
...
}
Hope it helps.

CakePHP 2.1 HTTP cache

I'm trying to speed up my site by taking advantage of the new HTTP cache features in CakePHP 2.1:
class ArticlesController extends AppController {
public function view($id) {
$article = $this->Article->find(
'first',
array('conditions' => array('Article.id' => $id))
);
$this->response->modified($article['Article']['modified']);
$this->set(compact('article'));
}
}
Caching works fine, but does not distinguish between different users (i.e. if a user logs in and visits a page that was already cached, the previously cached page is displayed, and user-specific content is not shown). I'd like one of the following to happen:
Cache discriminates between different users and stores a separate cache for each user
Caching is disabled if a user is logged in (the user login is only used for admin purposes)
I've tried adding
if (AuthComponent::user('id')) {
$this->disableCache();
}
But this doesn't seem to solve the problem
Does anyone know how to get this to work, or am I doing something fundamentally wrong?
You could try the etag caching method and generate a hash based on the article id and user id.
See http://book.cakephp.org/2.0/en/controllers/request-response.html#the-etag-header
The Etag header (called entity tag) is string that uniquely identifies the requested resource. It is very much like the checksum of a file, caching will compare checksums to tell whether they match or not.
To actually get advantage of using this header you have to either call manually CakeResponse::checkNotModified() method or have the RequestHandlerComponent included in your controller:
<?php
public function index() {
$articles = $this->Article->find('all');
$this->response->etag($this->Article->generateHash($articles));
if ($this->response->checkNotModified($this->request)) {
return $this->response;
}
...
}
I thought I'd post the solution(s) I eventually used, in case it helps anyone.
To disable caching completely for logged in users:
class ArticlesController extends AppController {
public function view($id) {
$article = $this->Article->find(
'first',
array('conditions' => array('Article.id' => $id))
);
if (!AuthComponent::user('id')) {
$this->response->etag($this->Article->generateHash($article));
}
$this->set(compact('article'));
}
}
To have a separate cache for each user (and for the case when no-one is logged in):
class Article extends AppModel {
public function generateHash($article) {
if (AuthComponent::user('id')) {
return md5(AuthComponent::user('id') . '-' . $article['Article']['modified']);
} else {
return md5($article['Article']['modified']);
}
}
}
class ArticlesController extends AppController {
public function view($id) {
$article = $this->Article->find(
'first',
array('conditions' => array('Article.id' => $id))
);
$this->response->etag($this->Article->generateHash($article));
$this->set(compact('article'));
}
}

CakePHP: Prevent Auth component's "authError" message on homepage

I have a CakePHP project where I modified "app/config/routes.php" so that the root points to the "Users" controller's "dashboard" action. In other words, these two URLs go to the same place:
http://example.com/
http://example.com/users/dashboard
I have the "Auth" component set up in my "App" controller like so:
class AppController extends Controller {
var $components = array('Auth', 'Session');
function beforeFilter() {
$this->Auth->authorize = 'controller';
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
if ($this->Auth->user()) {
$this->set('logged_in', true);
}
else {
$this->set('logged_in', false);
}
}
}
I want it so that if a non-authenticated user goes straight to http://example.com/users/dashboard , they are taken to the login page with the "Auth" component's "authError" message showing, but if they go to http://example.com/ , they are taken to the login page without the "Auth" component's "authError" message showing. Is this possible?
I resolved this by putting the following code in my "Users" controller's "login" action:
if ($this->Session->read('Auth.redirect') == $this->webroot && $this->Session->read('Message.auth.message') == $this->Auth->authError) {
$this->Session->delete('Message.auth');
}
been looking for somthing like that for a long time! Thank you.
I had to make a little change then $this->webroot is not "/":
if (str_replace("//","/",$this->webroot.$this->Session->read('Auth.redirect')) == $this->webroot && $this->Session->read('Message.auth.message') == $this->Auth->authError) {
$this->Session->delete('Message.auth');
}
Well, I don't understand why sometimes you show the error and why sometimes not.. but you can afford this creating an isAuthorized method and modifying all the logic of the default AuthComponent behavior.
Open your Auth component and check for method "startup()". There, at it's last line, you will se this:
$this->Session->setFlash($this->authError, $this->flashElement, array(), 'auth');
$controller->redirect($controller->referer(), null, true);
This is the part responsible for displaying the error.
Before it, you will se...
if ($this->isAuthorized($type)) {
return true;
}
So you can change your isAuthorized method to change this message when you want.
Is a lot of work for (I think..) nothing.
PS. There may be a simpler way to be ignoring me
If you really wants to prevent authError message on homepage and simple redirect to login page then you have to put false as parameter of authError
class AppController extends Controller {
public function initialize() {
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authError' => false
]);
}
}

Cakephp - how to make error pages have its own layouts?

I wanna have a different layout for the page not found 404 page. How can i set a different layout for that page?
Savant from the IRC helped me out and he suggest in using beforeRender(){} in the app_controller
// Before Render
function beforeRender() {
if($this->name == 'CakeError') {
//$this->layout = 'error';
}
}
CakeError is a catchAll for errors :D
In CakePHP 2.2.2 I changed the ExceptionRenderer in core.php with my own, like this:
app/Config/core.php:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'MyExceptionRenderer', // this is ExceptionRenderer by default
'log' => true
));
app/Lib/Error/MyExceptionRenderer.php:
App::uses('ExceptionRenderer', 'Error');
class MyExceptionRenderer extends ExceptionRenderer {
protected function _outputMessage($template) {
$this->controller->layout = 'error';
parent::_outputMessage($template);
}
}
Just you need to make layout changes in your error400.ctp file under /app/View/Errors/error400.ctp
Open that file and set layout by
<?php $this->layout=''; //set your layout here ?>
better to create an error.php file in your app folder
class AppError extends ErrorHandler {
function error404($params) {
$this->controller->layout = 'error';
parent::error404($params);
}
}
so you can avoid the if-testing at EVERY page render that savants' solution introduces
My solution for CakePHP 2.3
Change the ExceptionRenderer in core.php to use your own renderer.
app/Config/core.php:
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'MyExceptionRenderer',
'log' => true
));
app/Lib/Error/MyExceptionRenderer.php:
App::uses('ExceptionRenderer', 'Error');
class MyExceptionRenderer extends ExceptionRenderer
{
/**
* Overrided, to always use a bare controller.
*
* #param Exception $exception The exception to get a controller for.
* #return Controller
*/
protected function _getController($exception) {
if (!$request = Router::getRequest(true)) {
$request = new CakeRequest();
}
$response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
$controller = new Controller($request, $response);
$controller->viewPath = 'Errors';
$controller->layout = 'error';
return $controller;
}
}
The advantage to this approach is that it ensures any exceptions thrown from AppController don't cause an endless loop when rendering the exception. Forces a basic rendering of the exception message every time.
This simplest way I know of is to create this function in your AppController:
function appError($method, $messages)
{
}
You can then do whatever you want with the error, display it however you like, or not display it at all, send an email etc.. (I'm not sure if this method if still valid.)
There is also an option of creating app_error.php in your app root, with class AppError extends ErrorHandler in it, which enables you to override all kinds of errors. But I haven't done this yet, so I can't tell you more about it.
See cake/libs/error.php and cake/libs/object.php and of course The Book for more info.
Edit: Forgot to mention, once you caught the error, there's nothing preventing you to - for example - store the error in session, redirect to your "error handling controller", and then display it in your controller however you want.

Resources