I've got a problem showing $this->Session->flash();. In my controller just for testing purpouse I set a session flash and then I throw a new Exception, but in my view the session flash is not showing.
I'm throwing an error because I'm using ajax to submit a form and dynamically uptade my view. Is there a way to upcome this without droping the ajax?.
edit
in my controller
$this->Session->setFlash(__('Error, try again'), 'flash_foundation_good');
throw new InsertException(__('Error, try again.'));
in my layout
<div class="inner-wrap">
<?php echo $this->Session->flash(); ?>
<?php echo $this->fetch('content'); ?>
<a class="exit-off-canvas"></a>
</div>
In my view
As I said before, I'm using an ajax call here is a little of it. (just the submit button)
echo $this->Js->submit('Save',
array(
'id'=>'submit-ajax',
'update' => '#myModal',
'before'=>'before();',
'complete'=>'reset();',
'success'=>'success();',
'error'=>'nope(XMLHttpRequest);',
'div'=>array('class'=>'row'),
'class'=>'button',
'style'=>'margin: 0 auto; display: block;'
)
);
echo $this->Js->writeBuffer();
I just work around the session flash using the error method, and dynamically show what I want, but still I want too know how to make appear the session flash ):
Related
I have an application where I need to redirect to a website ( example below). My issue is that the cakephp3 application is embedded in a wordpress iframe and for some reason the below command executes inside an iframe so i have a webpage within a webpage. How can i redirect to a webpage outside an iframe?
//controller
if ....
return $this->redirect('http://www.xxxxxx/thank-you-application/');
What goes on in Vegas, stays in Vegas
What goes on in an iframe, stays in that iframe
I don't think what you want is possible with PHP (and any PHP framework),
Maybe with getting the content of the page that you want to load inside the iframe with:
$http = new Client();
$response = $http->get('http://example.com');
$content = $response->body();
and put the $content in the view in the "top level page / mean not inside iframe" (like they do in financial website), but i'm not sure of the exact way.
the easiest solution is to send in your controller a value ($redirect = true) to the view that will say: "Hey! view, plz open this link to the top level window! / mean outside the iframe".
write something like this in your view (or template):
<!-- ... -->
<?php if($redirect) : ?>
<body onload="javascript:window.top.location.href='<?= $this->Url->build([
"controller" => "Pages",
"action" => "display",
"thank-you"
]) ?>'";>
<?php else: ?>
<body>
<?php endif; ?>
<!-- ... -->
Hope it helps
In CakePHP 2.x you could do
AuthComponent::user()
in View to get data from Auth component. In CakePHP 3.0beta3 it throws:
"Error: Class 'AuthComponent' not found"
Is there a simple way to get data from AuthComponent in View?
In View:
$this->request->session()->read('Auth.User.username');
In Controller
$this->Auth->user('username');
You should have never used the AuthComponent in views in the first place.
It is better to either pass down the data from the controller to the view and access that, or better yet, use a AuthHelper to wrapper-access it easily (by reading from the session there for example).
An example would be AuthUser (
https://github.com/dereuromark/cakephp-tools/blob/master/src/View/Helper/AuthUserHelper.php ):
$this->AuthUser->id();
$this->AuthUser->user('username');
etc
The helper way doesn't require additional use statements in your view ctps and keeps them lean.
It also prevents notices when trying to access undefined indexed automatically.
if ($this->AuthUser->user('foobarbaz')) { // no error thrown even if it never existed
}
Cake 3.5
In AppController:
public function beforeRender(Event $event) {
....
$this->set('Auth', $this->components()->__get('Auth'));
}
In .ctp template:
<?php if (!$Auth->user()) { ?>
<a class="login" href="<?php echo $this->Url->build($Auth->getConfig('loginAction')); ?>">Login</a>
<?php } else { ?>
<div class="name"><?php echo h($Auth->user('name')); ?></div>
<?php } ?>
The AuthComponent is deprecated as of 4.0.0 and will be replaced by the authorization and authentication plugins.
Authentication plugin has built in View Helper.
In CakePhp 2.0, using CakeEmail new Component seems to not output flash message:
In my controller I put:
$email = new CakeEmail(array('log'=>true));
$email->transport('Debug');
and in my view
echo $this->Session->flash('email');
But nothing is printed out.
Has that function (flash) been removed in 2.0?
none of the cake email libs or components or transport classes touch the session or write any such flash content. they never did as far is I know.
but they return the email content as array for the DebugTransport.
so you would want to fetch the returned array and log it away:
$res = $this->Email->send();
$this->Session->setFlash($res ? 'Email sent' : 'Email not sent');
or sth like that.
Of course there is flash function in cakephp 2.0 for details check it here: http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
If you want to get ur flash message in your view you have to first set it in your Controller action.
//controller
$this->Session->setFlash('email');
//view
echo $this->Session->flash();
// The above will output.
<div id="flashMessage" class="message">
'email'.
</div>
In Cake 2.x the debug transport doesn't set the email content in session. Just check the return value, $contents = $email->send();. $contents will contain the headers and message so use them as required.
$response = $Email->send();
$response['headers']; // headers as string
$response['message']; // message body with attachments
$this->Session->setFlash($response['headers'].$response['message']);
Make sure you have the following in your layout file.
echo $this->Session->flash();
In in the CakePHP SDK for Facebook, the following code is provided for logging out:
<?php echo $this->Facebook->logout(array('redirect'=>array('controller'=>'users','action'=>'logout'))); ?>
This codes doesn't seem to work, as the API seems to have changed to now use "next" as opposed to redirect. So the above code doesn't produce a logout button; just a link;
The following code produces a logout button, but doesn't actually redirect:
<?php echo $this->Facebook->logout(array('next'=>array('controller'=>'users','action'=>'logout'))); ?>
Any idea what needs to be done to address this problem?
Try this
$params = array( 'next' => 'https://www.myapp.com/after_logout' );
$facebook->getLogoutUrl($params); // $params is optional.
Source: http://developers.facebook.com/docs/reference/php/facebook-getLogoutUrl/
The problem is i want to call the index function, i need it to render the view and then the
afterFilter to redirect again to the index function and do the same.. Like a loop, the problem is it doesnt render it, ive tried using $this->render('index') but it doesnt work and also other things..
PS: I didnt include all the code that i have in index, because its pointless, doesnt render with or without it, just included the things i needed for the view.
function afterFilter()
{
if ($this->params['action'] == 'index')
{
sleep(3);
$this->redirect(array('action'=>'index',$id), null, true);
}
}
THE FUNCTION
function index($ido = 0)
{
$this->set('Operator', $this->Operator->read(null, $ido));
$this->set('ido', $ido);
}
THE VIEW = INDEX.CTP
<legend>Operator StandBy Window</legend>
<?php
?>
</fieldset>
<?php echo $html->link('LogIn', array('controller' => 'operators', 'action' => 'add')); ?>
<?php echo $html->link('LogOut', array('controller' => 'operators', 'action' => 'logout',$ido)); ?>
a function that constantly checks my database for a change, if a change occurs ill redirect, if not i need to have that constant loop of 'checking the database' and 'rendering the view'.
This is not possible entirely on the server with PHP, and especially not with CakePHP's template system. PHP just "makes pages" on the server and sends them to the browser and a linear fashion. If you loop on the server, the content of your page will just repeat itself:
Normal content
Normal content
Normal content
<redirect>
To redirect the client, you need to output headers. The headers need to be output before anything else. If you've already looped a few times and content has already been sent to the client, you can't redirect anymore.
There are two ways:
You just output one page at a time showing the current status, with a meta tag that'll refresh the page every x seconds. This can be rather expensive and annoying though.
Use AJAX and possibly Comet to update the information on the page dynamically in the browser.