Facebook Logout / CakePHP Plugin - cakephp

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/

Related

CakePHP 3.x - AuthComponent::user() in View

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.

Exception and SessionFlash Cake

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 ):

CakeEmail: no render Flash Message

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

Outputting a hyperlink from a controller in cakePHP

I'm just getting started with cakePHP, and things aren't going so well so far.
I have a controller that handles confirming user emails. On registration the user is sent an email with a confirmcode in a link. Depending on the confirm code they give, the controller gives different text responses. One of these responses includes a hyperlink in order to log in.
I'm trying to use the Html helper, but although I've loaded it in $helpers at the top of the class, I an only make it work if I then use App::import, and then instantiate it.
It all seems overkill to simply make a hyperlink! How many times do I have to load the same class?
Wherever I look on the web it keeps telling me it's a bad idea to use a helper in a controller, but how else am I supposed to get the link made?
So I have
var $helpers = array('Html');
at the top of the controller, and:
if (isset($this->User->id)) { // Check the user's entered it right
// Do some stuff to remember the user has confirmed
// This is to load the html helper - supposedly bad form, but how else do I make the link?
App::import('Helper', 'Html');
$html = new HtmlHelper();
$this->set('message', __("Your email address has been confirmed.", TRUE)." ".$html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" )));
} else {
$this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}
in the controller's confirm method and
<div>
<?php echo $message;?>
</div>
in the view to output the resulting message
Surely I'm going wrong somewhere - can anyone explain how?
You're not supposed to use Helpers in the Controller. As #Lincoln pointed out, you should construct the link in the View. You may construct the URL in the Controller, since a URL is basically data, but a link is a very medium-specific (HTML) implementation of a URL.
Either way, you'll need to create a full URL (including host) if you want to send it in an Email. The most universal way is to use Router::url:
$fullUrl = Router::url(array('controller' => ...), true); // 'true' for full URL
Do this in either the Controller or the View. For creating a link, use this in the View:
echo $html->link('Title', $fullUrl);
The idea is that all the data you need to render the page is sent to the view with set, then any conditional logic or formatting is done in the view with helpers, so send whole query results when appropriate (suppose you need to alter a link to include the user's screen name, you'll have it handy).
in controller action
$this->set('user', $this->User);
in view (this is slightly different depending on if your in <= 1.2 or 1.3
if ($user->id) //available because of Controller->set
{
//1.2
$link = $html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
//1.3
$link = $this->Html->link(__("Please log in", TRUE), array('controller' => "users", 'action' => "login" ));
echo __("Your email address has been confirmed.", TRUE)." $link";
}
else
{
$this->set('message', __("Please check your mail for the correct URL to confirm your account", TRUE));
}
What you are trying to do should be done with the SessionComponent. $this->Session->setFlash('your message here');
and in your layout with the session helper put $this->Session->flash();
About your wanting urls in the controller, Router::url is correct as deceze said, but there is no use for it there as you should not be building html in a controller.
what you want to do it use the session::setFlash() method above and then redirect them using
$this->redirect(array('controller' => "users", 'action' => "login" ));

CakePhp - render a view + afterFilter

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.

Resources