Why doesn't my CakePHP flash message appear? - cakephp

Background: I'm new to CakePHP. I have a small test site (mostly composted of static views and a contact form at the moment) to play with while I learn. Everything worked fine on localhost (Apache on Ubuntu) so I deployed it to a shared hosting account (provided by Lunarpages). I moved the /cake folder out of the normal directory structure (so I could use it for multiple apps) and I reconfigured my webroot's index.php paths accordingly.
Problems:
setFlash messages do not get displayed. Even making a simple view that does nothing other than $this->Session->setFlash('message');. I don't get any error message, the flash just doesn't get displayed
Redirects don't work. For instance, after the contact form is completed I want to $this->redirect( array( 'action' => 'success' ), null, true); but the server throws an error:
Warning (2): Cannot modify header information - headers already sent by (output started at /routetoapp/config/routes.php:40) [CORE/cake/libs/controller/controller.php, line 742]
Everything else seems to work just as it did on localhost - URL rewriting, component loading, model validation. I don't know if my problems are related or separate issues
Troubleshooting so far:
I've tried both 'cake' and 'php' for Configure::write('Session.save', 'val'); but neither made a difference.
My app/tmp folder is writeable.
My layout template has the correct code for displaying flash messages. (The exact same M, V, C, and Layout objects display the expected flash on localhost)
I assume I'm missing something simple, but I'm new to this framework so I'm not sure where else to look.

See Matt Huggins answer for your flash problem. That's correct
As for your redirect issue,
you might have an extra space or something in your routes.php file. Make sure there are no spaces before the starting <?php tag and remove the closing ?>

$this->Session->setFlash(...) is used to SET the flash message from within the controller. When you're in the view, you should be rendering the flash message like this:
<?php $session->flash(); ?>
You can also make your flash message more elaborate if you need with something like this:
<?php if ($session->check('Message.flash')): ?>
<div class="message">
<?php $session->flash(); ?>
</div>
<?php endif; ?>

That was right but there also have echo add before $session->flash();
so, it should be like:
<?php if ($session->check('Message.flash')): ?>
<div class="message">
<?php echo $session->flash(); ?>
</div> <?php endif; ?>
this worked for me!

Related

Cakephp echo username into page title

I am currently learning Cakephp and have made a profile page of which displays the username on it. You can only access this page when logged in so when you are on this page, I wish to display the username of the user in the HTML page title. This is also because your profile page is visible to other users.
At the moment to get the page headers I have used.
<title><?php echo $this->fetch('title'); ?> | Site Name</title>
<? $this->assign('title', 'Profile')?>
And to display the username on the page currently I have:
<?php echo $user['User']['user_name']; ?>
I have attempted to combine these two into something like shown below but clearly that's not working. Any ideas?
<? $this->assign('title', '<?php echo $user['User']['user_name']; ?>')?>
Thanks!
Mistake in the concatenation, change this in your .ctp
<? $this->assign('title', '<?php echo $user['User']['user_name']; ?>')?>
to this
<? $this->assign('title', $user['User']['user_name'])?>
Later on add this in your layout
<title><?php echo $this->fetch('title'); ?> | Site Name</title>
This will work :)

Custom error pages are not showing

I have a custom error page for 500 Server Errors in CakePHP 3.
My src\Template\Layout\error.ctp template file has been adjusted to have my default error template in it. And then I've also updated my view file at src\Template\Error\error500.ctp. I even added this line, $this->layout = 'error'; as described in the docs.
This is what my error view looks like:
<?php
use Cake\Core\Configure;
use Cake\Error\Debugger;
$this->layout = 'error';
$this->Html->addCrumb('Error');
?>
<section class="content">
<div class="error-page">
<h2 class="headline text-red">500</h2>
<div class="error-content">
<h3><i class="fa fa-warning text-red"></i> Oops! Something went wrong.</h3>
<p>
We will work on fixing that right away.
Meanwhile, you may return to dashboard or try using the search form.
</p>
</div>
</div>
</section>
However, it still shows the default CakePHP error page.
What am I missing?
Just turn off the debug in app.php like
'debug' => false
As it written in cookbook
Each framework layer exception has its own view file located in the core templates but you really don’t need to bother customizing them as they are used only during development. With debug turned off all framework layer exceptions are converted to InternalErrorException
For detailed info about ExceptionRenderer visit here

cakephp custom flash layout error

i have looked at the documentatioin of cakephp 2.5.4 about custom flash messages and some other tutorials on how to make this but its not working
i have error.ctp and success.ctp under elements folder
error.ctp
<div id="error-flash">
<?php echo h($message); ?>
</div>
success.ctp
<div id="success-flash">
<?php echo h($message); ?>
</div>
and in controller i call them like this
$this->Session->setFlash(__(' file successfully uploaded.','success',array('class'=>"flash_msg_ok")));
$this->Session->setFlash(__('Upload Failed','error',
array("class" => "flash_msg_error")));
my chrome developer tools still shows the default flash generated.
its not rendering the element or applying the specified classes
i've also tried this
$this->Session->setFlash('success','default',array('class'=> 'success'))
still nothing.
of note is that am using custom layout and css for the page.
what am i missing here?
There are a couple of issues..
First, you've accidentally put the other function variables inside the translation string. You can fix the call as follows (note closing bracket moved to end of message string):
$this->Session->setFlash(__(' file successfully uploaded.'),'success',array('class'=>"flash_msg_ok"));
$this->Session->setFlash(__('Upload Failed'),'error',
array("class" => "flash_msg_error"));
Second, you'll need to add the class to your custom error element file (the cakephp docs neglect to mention this!). For example:
<div id="error-flash" class="<?php echo $class; ?>">
<?php echo h($message); ?>
</div>
Hope that helps!

AppError 404 equivalent in cake 2.0

I've recently upgraded my 1.3 cake app to 2.0, and I'm trying to redo my app_error code.
With Cake 1.3, it was simply a case of creating an app_error.php file, putting it in my app root, and overriding the built-in error404() and missingController() actions.
Here is my old 1.3 /app/app_error.php file: http://pastebin.com/beWZD9PJ
It had some code that kicked in when someone arrived at the site with a predefined 'alias' URL, and then it redirected them accordingly.
I just need this to work in Cake2.0, and I see the manual is telling me its all changed, but I can't find a specific case like this. Can anyone help me out, so the error404 code kicks in?
Many thanks
4xx and 5xx errors in CakePHP 2.0 ar now exceptions, thus you need to customize your Exception renderer or handler. Read from this section to the bottom http://book.cakephp.org/2.0/en/development/exceptions.html#exception-renderer
You can throw a 404 exception with the following code in your controller in Cake 2:
throw new NotFoundException(__('Your error text here'));
Then, you just need to have APP/View/Errors/error400.ctp present with something relevant to display. Cake includes this by default:
<h2><?php echo $name; ?></h2>
<p class="error">
<strong><?php echo __d('cake', 'Error'); ?>: </strong>
<?php printf(
__d('cake', 'The requested address %s was not found on this server.'),
"<strong>'{$url}'</strong>"
); ?>
</p>
<?php
if (Configure::read('debug') > 0 ):
echo $this->element('exception_stack_trace');
endif;
?>
It's simple.
With cakephp 2, you have to add this function in your AppController:
public function appError($error) {
$this->redirect('/');
}
Here, for example we redirect all errors to index page.

cakephp redirect doesn't work on server

On my PC with WAMP server and php 5.3.9 everything works fine.
When I upload it to server with php 5.2.1.7 all redirects stop working - when the ->redirect(..) is executed script stops working - it acts like there was die; instead of redirect and nothing is printed and redirect doesnt work.
These are redirects I am using:
$this->redirect( array('controller' => 'users', 'action' => 'login') );
$this->redirect( $this->referer() )
Both (in fact all...) stopped working after upload to server...
------ edit
I managed to show E_ALL errors and for example if I write $omg->lol() before redirect the error is reported as
Notice (8): Undefined variable: omg [APP/Controller/LanguagesController.php, line 31]
Fatal error: Call to a member function lol() on a non-object in ...
But still no error message for redirect...
I managed to fix it!
The problem was that few php files had some tabs or whitespaces before <?php tags and after ?> tags - when I deleted them everything works fine - damn PHP is real bitch!!!

Resources