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

Run initial RBAC migrations as part of a regular app migration
Yii2 - Make Form Validator For URL Type To Skip “http://” in the start
How to create Yii2 Breadcrumbs
create an expandable grid view in Yii2
Yii2 - Get Current Action ID in main.php
Yii2 Validate Only Create Action
Yii2 preselect radiobutton value
how to set URL (admin instead of backend/web) in yii2 advanced application
yii2 AR find via 2 relative tables
Form validation rule does not render custom message
Class not found in _before function of Codeception TestCase
YII2 Gridview doesn't return selected rows (Uncaught TypeError: Cannot read property 'selectionColumn' of undefined yii.gridView.js:140)
Remove query string to Yii URL
cant get data was Post from android to Yii2 action
Loading part of form via ajax. Extra form tag. Yii2
How to Using Yii2 BaseImage class crop image from center

Categories

HOME
arduino-uno
deployment
isabelle
lodash
android-4.4-kitkat
getelementsbytagname
elm
maven-3
electronics
packages
umd
webpack-2
ravendb
floating-action-button
graphlab
windows-7-x64
serilog
http-status-code-504
crystal-reports-2008
emulator
scriptcs
restful-authentication
url-scheme
wpfdatagrid
pingfederate
nhapi
tooltipster
windows-error-reporting
social-media
maxmind
ping
compatibility
gitignore
primitive
bluestacks
ecto
bosh
airconsole
kendo-ui-grid
form-data
host
plsql-psp
restlet
dynamics-crm-2013
occlusion
starteam
elmah
qwt
react-scripts
integrity
imanage
objective-c-swift-bridge
word-vba-mac
asp.net-4.5
thrust
blackberry-10
actionbardrawertoggle
testng-dataprovider
livequery
asp.net5
iad
wso2cloud
lemon
sdhc
eclipse-clp
sframe
sniffer
oberon
pick
phalanger
rdl
expected-exception
mechanize-ruby
undefined-reference
multiprocessor
tidy
xamlparseexception
limejs
angularjs-controller
jspinclude
assembly-loading
hamachi
jmock
asp.net-mvc-areas
vc90
webkit.net
memory-size
uimenucontroller
eqatec

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App