flash message and redirect in cakephp3 - redirect

I have the below code in one of my controller files.$this->Flash->error("error message!");
return $this->redirect(['controller' => 'users', 'action' => 'home']);.My problem is, flash message is not displaying on the redirected page. Can anyone help?

in controller:
$this->Flash->error("error message!");
return $this->redirect(['controller' => 'users', 'action' => 'home']);
in your home.ctp or layouts/default.ctp
<?= $this->Flash->render() ?>
** in your Element/Flash**
create error.ctp if . not exists

Related

No redirect after login via Facebook Login CakePHP

I am following the tutorial to apply FB login in my application. I have completed whole tutorial step by step. There is also button visible on login page "Login via Facebook". But when I click on that button and got autheticated by FB or I am already logged into FB but my page is not redirecting to logged in area. Even after login via FB I remain at login page. But CakePHP login works normal.
Some code snippet from login action of UsersController.php
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
if($this->Auth->user('id')==1){
if($this->Auth->user('type')==1){
if($this->Auth->user('verified')==0){
$this->Auth->logout();
}
}
return $this->redirect($this->Auth->redirectUrl());
$this->Session->setFlash(__('Logged In Successfully'), 'default', array('class' => 'alert alert-success'));
}
else{
$this->redirect(array('controller'=>'users','action'=>'index'));
}
}
$this->Session->setFlash(__('Invalid username or password, try again'), 'default', array('class' => 'alert alert-danger'));
}
}
In AppController.php I have included following code:
public $components = array(
'Paginator',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'users',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'home',
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields'=>array('username'=>'email')
)
)
),
'Email',
'Facebook.Connect'
);
var $helpers = array('Facebook.Facebook');
In UsersContoller.php pr($_SESSION) output following code when I am already logged in FB on CakePHP login page:
Array
(
[Config] => Array
(
[userAgent] => f04e6*****************c00392
[time] => 1425647071
[countdown] => 10
)
[fb_989554394440625_user_id] => 8459*********71
)
I have searched around the web for this issue but unable to get it solved.
There is missing some important part of code needed to give an qualified, verified answer..
Where are you calling the login-Function? Ensure you only call this if user is NOT logged in, otherwise render your stuff..
if(!$this->Auth->user()) {
$this->login(); // request login
} else {
$this->render('profile'); //render your view
}
This should render the "profile"-View if a user was logged on correctly. If you seem to be logged in but the login-Fn still gets called, you probably made a mistake in overriding Auth-Methods (if you did so)
If you still cannot solve the issue put some more code here.

Cakephp2 post to plugin controller

Having hard time figuring out how to post a form to a plugin controller.
Say I have a reg/login plugin that could be shared with different apps.
in reg.ctp:
echo $this->Form->create(null, array('url'=>array('controller' => 'user', 'action' => 'submit','plugin'=>'user')));
I get missing controller.. in app/controller/user.php
what did do wrong?
Looks like your controller is not pluralised. All controllers in Cake, by convention, are plurals.
array('controller' => 'users', 'action' => 'submit', 'plugin' => 'user');
Should route to
app/Plugin/User/Controller/UsersController::submit()

Redirection from module to application

how can we go back form one module to parent application in ZEND. I use this code:-
<?php echo $this->url(array('controller' => '', 'action' => '')); ?>
If you add a 'module' key to the URL array, you can easily traverse modules. If you're trying to get to the "Parent" (the default module, perhaps?), you'd change your code to:
<?php echo $this->url(array('module' => 'default', 'controller' => 'index', 'action' => 'index)); ?>

Can't get Auth login to work with CakePHP 2.0

I'm trying to get a simple login form to work using CakePHP 2.0... just Auth, no ACLs for now.
I'm able to see the form and enter the email and password as they are in the database, but I just get returned to the form and the flash error message is displayed. Here is my code:
AppController:
class AppController extends Controller
{
function beforeFilter()
{
$this->Auth->userModel = 'Users';
$this->Auth->fields = array('username' => 'email', 'password' => 'password'); //have to put both, even if we're just changing one
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'hotels', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
}
}
login.ctp:
<?php
echo $this->Form->create('User', array('action' => 'login'));
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
UsersController:
class UsersController extends AppController
{
var $name = 'Users';
var $helpers = array('Html','Form');
var $components = array('Auth','Session');
function beforeFilter()
{
$this->Auth->allow("logout");
parent::beforeFilter();
}
function index() { } //Redirects to login()
function login()
{
if ($this->Auth->login())
{
$this->redirect($this->Auth->redirect());
} else
{
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
function logout()
{
$this->redirect($this->Auth->logout());
}
}
?>
I appreciate any help with this. Thanks!
The "Invalid username or password, try again" error is displayed after you hit login?
There are a few things you should check:
• Is the output of $this->Auth->login() identical to the information in your database? Put debug($this->Auth->login()) to see the output in your login method after the form is submitted.
• Are the passwords correctly hashed in the database?
• Try making the AuthComponent available to all your controllers not just the UsersController.
• Not sure if this makes a difference, but call parent::beforeFilter(); before anything else in your controller's beforeFilter method.
EDIT:
Is see that you're trying to validate based on email and password. As a default AuthComponent expects a username and password. You have to explicitly state that you want the email and password to be validated by $this->Auth->login(). This comes from the 2.0 documentation:
public $components = array(
'Auth'=> array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
The fact that you're not seeing any SQL output is to be expected, I believe.
Also you must check if your field "password" in database is set to VARCHAR 50.
It happens to me that I was truncating the hashed password in DB and Auth never happened.
if you are not using defalut "username", "password" to auth, you cant get login
e.g., you use "email"
you should edit component declaration in your controller containing your login function:
$component = array('Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email', 'password' => 'mot_de_passe')
)
)
));
Becareful with cakephp's conventions. You should change this "$this->Auth->userModel = 'Users';" to "$this->Auth->userModel = 'User';" because User without plural is the Model's convention in cake. That worked for me and also becareful with the capital letters. it almost drived me crazy. Good luck.
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'Events',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'Users',
'action' => 'login',
'home'
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username','password' => 'password')
)
)
)
);
Editing the component declaration in AppController did the trick for me. If you have the fields named other than "username" and "password" you should always specify them. In your case it would be
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username' => 'email','password' => 'password')
)
)
)
);
There is a bug in the cakephp tutorial.
$this->Auth->login() should be changed to
$this->Auth->login($this->request->data)

Zend Navigation, how can i use it with params in the url ? I tried it with params in navigation without changes :(

I have got a problem with the zend navigation.
I use the zend navigation, and its ok when i call something like this in the url:
www.website/article_new, www.website/article_list,
www.website/friends_new, ...
But I want to call the site with some params like this:
www.website/article_new/123452/2335/45633246,
www.website/friends_new/23453/3453524/34554
I try it about to set some params, but this doesnt function.
I put the navigation in the bootstrap and in the layout I use it. Its a Navigation with some breadcumps where display:true and display:false.
Here is a code from my bootstrap:
$navigation = new Zend_Navigation(array(
array(
'label' => 'Home',
'controller' => 'index',
'action' => 'index',
'class' => 'menuF'
),
array(
'label' => 'Article',
'controller' => 'Article_List',
'id' => 'article',
'action' => 'index',
'class' => 'menu',
'pages' => array(
array(
'label' => 'Neu',
'id' => 'article',
'controller' => 'Article_New',
'action' => 'index',
'class' => 'submenu'
)
)
).....
And the code from the layout.phtml. On this code I show if the breadcump is shown or not.
foreach($container as $page) {
$par = $active;
if ($page != $active && $page->getClass()!='menu' && $page->getClass()!='menuF' && $page->getID()!=$par->getID()) {
$page->setClass('submenu');
} else {
$found = $container->findAllBy('ID', $active->getID());
foreach($found AS $p){
if($p->getClass()!='menu'&&$p->getClass()!='menuF'){
$p->setClass('sub');
}
}
}
}
echo $this->navigation()->menu()->renderMenu($this->nav);
Hope anybody can help me!
Thanks for all!
Best regards
Tom
I found myself the mistake!
I want to call something like this:
www.website/article/344435/23452345
surely this throws a mistake, because there isnt any action like 344435
with something like this
www.website/article/show/var1/2435234/var2/2345234352
its ok.
the rest is something for routing!
Big sorry to all!
Greats Tom
Try to use the route name instead of the module, controler and action system.
array(
'label' => 'Article',
'route' => 'Article_List',
'id' => 'article',
'class' => 'menu',
...
)
The router will handle all the URL parsing and return the correct variables.