Chtml link failing - yii

I try to link from the site controller to another controller action using the Chtml::link, but it keeps redirecting me to the site/login page.
This code I have in the login view rendered from the site controller looks something like this:
<?php $this->beginWidget('system.web.widgets.CClipWidget', array('id'=>'Aboutinfo'));
.
.
<?php echo CHtml::link('Learn more here', array('site/page','view'=>'about')); ?>
.
.
echo CHtml::link('Link label', array('othercontroller/action'));?>
.
.
<?php $this->endWidget();?>
Thanks for any suggestions/corrections for what I might be doing wrong

Check the accessRules function in the controller you are trying to link to. You should see something like this:
public function accessRules()
{
return array(
array('allow',
'actions'=>array('index'),
'users'=>array('*'),
),
array('allow',
'actions'=>array('browse', 'add'),
'users'=>array('#'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
The 'users'=>array('*') means allow any user to access the action. The line 'users'=>array('#') means allow only logged in users to access the action (browse & add in the above case).
Make sure your rule settings are correct. I suspect the actions you are linking to have accessRules setup that require users to be logged in to view the page. If the controller detects that the user is not logged in, it will redirect user to the login page.

Related

After a successful login, I want to be redirected to the screen I was on before I logged in. getLoginRedirect() does not work

Suppose you were in the user list screen. From there you will go to the login screen to log in. There, you will enter your email and password and enter the submit button.
You have successfully logged in.
If the login is successful, we want to be redirected to the user list screen.
If you were in the user details screen, you will be redirected to the user details screen by
If the user was on the edit screen, the user editing screen
I have read the AuthenticationPlugin documentation at book.cakephp.org.
There I learned to use getLoginRedirect() to achieve this functionality.
I am aware that what I want to do will happen once I set up in the steps below.
However, getLoginRedirect() returns null.
What do I need to do?
What am I missing?
What's wrong?
// in Application.php
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$path = $request->getPath();
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => '/',
'queryParam' => 'redirect', // <- I believe this is the only one that matters.
]);
// Abbreviated below....
}
// in UsersController
public function login()
{
$this->request->allowMethod(['get', 'post']);
if ($this->request->is('post')) {
$result = $this->Authentication->getResult();
$requestData = $this->request->getData();
if ($result->isValid()) {
// I want to get the original url. But null is passed.
$redirect = $this->Authentication->getLoginRedirect() ?? '/';
return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('メールアドレス、またはパスワードが間違っています。'));
}
}
}
I think I've disclosed everything related to getLoginRedirect(). If there is something missing or something you are curious about, please feel free to let me know.
Please help me. Please help me...
The login redirect functionality provided by the plugin only works automatically when you are being redirected to the login URL after accessing a URL that requires authentication while not being logged in. In that case the authentication middleware will set the redirect query string variable with the current URL, so that the component can pick it up after the redirect to the login URL.
If you manually visit the login URL, then you'd also need to manually set the redirect query string variable, ie in your menu where you build the link that takes you to the login, add the current URL to the query string, something along the lines of this:
$service = $this->request->getAttribute('authentication');
// here `$queryParam` would by default be `redirect`
$queryParam = $service->getConfig('queryParam');
echo $this->Html->link('Login', [
'plugin' => null,
'prefix' => false,
'controller' => 'Users',
'action' => 'login',
'?' => [
$queryParam => $this->request->getRequestTarget(),
],
]);
So if you're on /users/show, the login link's URL would look something like:
/login?redirect=/users/show
and the form helper that builds your login form should pick up that exact URL, so that after submitting the form, the authentication component can read the redirect URL from the current URL accordingly.
See also
Cookbook > Views > Helpers > Html > Creating Links

How to add Forgotten Password text in Login with redirect to other page in Yii2

I need some help how to put the "Forgotten Password" to Login page to redirect to the page I want to go to after its clicked
Try this:
<?= Html::a(Yii::t('app', 'Forgotten Password?'),
['site/change-password'],
['class' => 'text-center'])
?>
In site Controller :
public function actionChangePassword()
{
return $this->render('ChangePassword');
}

Yii: The system is unable to find the requested action "select"

what i am doing is trying using the yii select2 extension to create a searchable dropdown list. i have downloaded the extension from this link and following it "http://www.yiiframework.com/extension/select2/".
i have put the unzipped file(which is select2) in protected/extensions and i have then created a php file in "protected/views/site/select.php" in which i pasted the code below and when i try to run it via "webapp/index.php/site/login" it gives this error "
Error 404
The system is unable to find the requested action "select"." Please help me with this, thankyou..!!
//code in select.php(protected/views/site/select.php)
$tags=array('Satu','Dua','Tiga');
echo CHtml::textField('test','',array('id'=>'test'));
$this->widget('ext.select2.ESelect2',array(
'selector'=>'#test',
'options'=>array(
'tags'=>$tags,
),
));
It seems that you have make the view file (protected/views/site/select.php) but you don't have created the corresponding action.
Add in SiteController:
public function accessRules() {
//You can modify accordingly but you have to insert select to allowable actions
return array(
array('allow', // allow all users to perform 'index', 'contact' and 'select' actions
'actions'=>array('index', 'contact', 'select'),
'users'=>array('*'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
public function actionSelect() {
$this->render('select');
}

can't display error page layouts in yii

I am able to render my specified error pages in my modules, but I can't get error layouts outside of my modules.
It's quite strange, in my main config I have:
'errorHandler'=>array(
'errorAction'=> '//site/error',
),
I tried to see if it is pointed to the right file by changing it to //site/contact, and see if it renders the contact page if error. And it does show.
So the path is correct, then how come it's not showing the error page? It shows the default white page with exception error. The exception page is what im expecting, but how it's formatted is not.
public function actionError()
{
$this->layout = '/layouts/main';
if($error=Yii::app()->errorHandler->error)
{
if(Yii::app()->request->isAjaxRequest)
echo $error['message'];
else
$this->render('error', array('error'=>$error));
}
}
Your user role doesn't have rights to access actionError() in controller.
In your site controller, You can see a function called public function accessRules().
Check error is added for some user role, or it's for admin only.
If error not found, then add it for user roles.
Example:
public function accessRules()
{
return array(
array('allow',
'actions' => array('index', 'view', 'error'),
'users' => array('#')
),
..
..
..
}
You need to create a errorXXX in views/system. The XXX stand for the error code, in this case most likely a 403. For more information see this page on the Yii site.

Can we implement different login url for sessiontime out and accesscontrol in Yii?

Is it possible to impliment 2 login url in Yii.
that is if a sessiontimeout occurs user should be redirected to site/login.
but when an unauthenticated user tries to access the url he should be redirected to site/loginaccount
Is it possible?
this is the code in the config file for session timeout and initializing login url
'components'=>array(
'user' => array(
'class' => 'WebUser',
'loginUrl' => array('site/loginaccount'),
'allowAutoLogin' => true,
),
'session' => array(
'class'=>'CDbHttpSession',
'timeout'=>$params['session_timeout'],
'autoStart'=>true,
),
This is code in my controller, which redirects to site/loginaccount if user is Guest
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('Createdeal'),
'users'=>array('*')
),
A hacky way to do this would be to put something like this in your view (or your template):
<head>
<?php if (!Yii::app()->user->isGuest) {?>
<meta http-equiv="refresh" content="<?php echo Yii::app()->params['session_timeout'];?>;url=http://example.com"/>
<?php }?>
</head>
Obviously the URL could be generated using createUrl(). You could do something similar via javascript.
Obviously all the usual disclaimers about using meta refersh and javascript redirects would apply . . . .