yii2


Yii2 check if user is loggedin in yii2


I have a controller action that i would like to check if user is a guest in yii2 but the code fails even if the user is a guest this is what i have tried
class CustomerController extends Controller
{
public function beforeAction($action)
{
if(Yii::$app->user->isGuest){
return $this->redirect(Yii::$app->urlManager->createUrl("site/login"));
}
public function actionDashboard(){
//do dashboard stuff
}
}
I have also tried using accessControl filters but they still fail
This is the access control filter instance in a Frontend Controller
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'controllers' => ['customer'],
'actions' => ['dashboard'],
'allow' => true,
'users' => ['?'],
],
], // rules
], // access
]; // return
} // behaviors
Then on my customer controller i just extend Frontend Controlller but even with this approach it fails
BY DOING IT THIS WAY
public function actionDashboard(){
if(Yii::$app->user->isGuest){
return $this->redirect(Yii::$app->urlManager->createUrl("site/login"));
}else{
//perform dashboard stuff
}
}
This works but the problem is that by following the last approach there is alot of code duplication in the other actions which doesnt seem right even though it works
What is wrong with the latter approaches which seem better
I have checked on This link and also This second one and implementend them but none works
WHAT COULD BE WRONG.
AM using Yii2 improved advanced template
Have you read the guide at all? This is explained there in details...
Add this method in the controller that should be hidden from guests.
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['#'],
],
],
],
];
}
That's it.
Every action in controller with this behavior is accessible only by signed users. Guests are automatically redirected to the login page (default site/login).

Related Links

calculate average in yii2
yii2 stripe include error (class not found)
Restricting controller action to creator of post in Yii2
Yii2 Email How to set sender name
How to passing parameter to afterSave() Method in yii2?
search for multiple keywords in the same field in Yii2
What is function behaviors in SiteController.php?
In YII2 validation rules I need something opposite to compare
How to add sendgrid mailer function in yii2?
Yii2 dataprovider- exclude first 2 elements
Many to many relationships in Yii2
yii2 swiftmailer link does not work
Call to a member function saveAs() on null (I can not update if I do not select an image)
yii2 dataProvider- offset in dataprovider doesn't work [duplicate]
Yii2 Active Record get the latest record
how to show json formated data in GridView

Categories

HOME
ms-access
pypi
oracle11g
heroku
objectgears
read-eval-print-loop
xmpp
jsp-tags
gis
tizen-web-app
webpack-2
multiple-records
esper
tomcat6
modx-revolution
nstableview
moonmail
messages
ibm-odm
dynamics-crm-online
lldb
predix
orleans
hammerspoon
icloud-api
sqlcipher
web-api-testing
wtx
srcset
madlib
nouislider
bosh
phonegap
data-manipulation
pdb
google-qpx-express-api
vsts-build-task
lxd
asset-pipeline
repo
wallpaper
catalog
revolution-slider
disassembling
angularjs-factory
temporary-files
azure-ml
email-templates
jvm-languages
bootstrap-dialog
http-redirect
websphere-mq-fte
pnotify
mplayer
setter
lift-json
flow-control
nxt
bonobo
markojs
react-native-listview
lua-5.1
oauth2client
ptrace
msys
lemon
intellij-14
purge
ionic
tableau-online
splash
ios9.1
iiviewdeckcontroller
unity5.2.3
proj4js
pick
camanjs
device-manager
gulp-less
rdl
uitouch
system.net.webexception
hippomocks
dealloc
typo3-neos
terminal-services
access-rights
gridfs
gwt-rpc
sqlperformance
referrer
browser-detection
cisco-jtapi
coderush
multipage
krl
disclosure
actionview
infrastructure
icanhaz.js
mozilla-prism
jquery-ui-droppable
filtered-index
bespin
meego
ubuntu-9.04
msdev
windows-live-messenger
document-conversion
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App