how can I access frontend module from backend in yii2? - yii2

I have created 'contact_us' CRUD in frontend. Now I want to show listing of that contact_us (i.e. index.php) in backend. how can I access it?

You should add a new urlMangerBackend in urlManager component in your frontend application /config/main.php
and then refer to this in your createUrl
return [
....
'components' => [
'urlManager' => [
// your normal frontend URL rules
],
'urlManagerBackend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/yourapp/backend/web',
'enablePrettyUrl' => true,
'showScriptName' => true,
],
],
];
and you can invoke this way
Yii::$app->urlManagerBackend->createUrl();

Related

Controller / Action for guest users (Does not require authentication) using Yii2-user

I am using Yii2 (basic) and Yii2-user for a website with users. For most actions it's necessary to be authenticated. How could I make a controller / action accessible as a guest?
I have tried things like this in the guest's controller:
'rules' => [
[
'allow' => true,
'actions' => ['index', 'confirm', 'download-form', 'upload-form'],
]
],
And this should be enough. But nope. I suspect that it is Yii2-user module who gets in the way and always redirects me to login.
And I have added the module in the web.php configuration like this:
'components' => [
...
...
'user' => [
'class' => 'nkostadinov\user\components\User',
'identityClass' => 'nkostadinov\user\models\User',
'enableConfirmation' => false,
'as firstLoginPolicy' => [
'class' => 'nkostadinov\user\behaviors\FirstLoginPolicyBehavior'
],
],
],
Any idea?
I have solved it as follows.
In my web.php configuration I had this:
'modules' => [
...
],
'as access' => [
'class' => \yii\filters\AccessControl::className(),//AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'request', 'change-password'],
'allow' => true,
'roles' => ['?']
],
[
//'actions' => ['logout', 'index'], // add all actions to take guest to login page
'allow' => true,
'roles' => ['#'],
],
],
],
'params' => [ ... ]
So, I have added this new rule to grant guest users access to all actions of this controller:
[
'controllers' => ['mymodule/my-controller'],
'allow' => true,
],
And that's it.
i suggest you to use mdmsoft/yii2-admin for authentication

yii2 url manager rules accept not pattern

I need configure my Yii2 UrlManager rules like this:
change http://domain/site/action to http://domain/action
change http://domain/module/default to http://domain/module
so far what I have done:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<module:(!site)>' => '<module>/default',
'<action:\w+>' => 'site/<action>',
],
],
when I trying access module it return 404. But when I remove '<action:\w+>' => 'site/<action>', access module again will show as module/default page. So how to solve this?
You can try this code
'<action:(about|contact)>' => 'site/<action>',
insted of this
'<action:\w+>' => 'site/<action>',
and it will not work then change sequence of rules
'rules' => [
'<action:\w+>' => 'site/<action>', <-----------it will be first
'<module:(!site)>' => '<module>/default',
],

Yii2 hide action name from url

I'm trying to remove action name from url. 'post/view' to 'page' using urlManager but it's not working
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Hide index.php
'showScriptName' => false,
// Use pretty URLs
'enablePrettyUrl' => true,
'rules' => [
//'<view:\w+>' => 'post/<alias>',
//'<alias:view>' => 'post/<alias>',
'page' => 'post/view',
],
],
Change your config to include the id:
'page/<id>' => 'post/view'
Allows you to use urls like this:
localhost:8585/yii2basic/wfp/web/page/PGr1mtIkAE
You can't really do this:
localhost:8585/yii2basic/wfp/web/post/id=PGr1mtIkAE

How to create a pretty URL?

How can I create a pretty url like this
customer/index/amount/12000/location/in . In Yii1.1 it was available by default. it was easy to enable pretty url. In Yii2 if need pretty url I've to write rules for every action !!!
In Codeigniter you will get index.php/controller/action/parameter1/parameter2/parameter3
ie it does not expose action parameter variables, that too without writing any url rules!
===Edit===
pretty URL doesnt work for parameters if no rule is defined
below is my main.php
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [ '<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
i am getting a lot of 404's, like
192.168.1.3/~user/urshow/frontend/web/movies/movies_all
it would have work fine if it would be like this
192.168.1.3/~user/urshow/frontend/web/index.php?r=/movies/movies_all
Go to config.php and add
'components' => [
...
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
...
],

how to set URL (admin instead of backend/web) in yii2 advanced application

I am facing a grate problem in my project.
When i browse url with my project name than it's show folders, but i don't want to show those folder i want to show direct my web site, like 'localhost/mywebsite/admin'.
I also try to solve this problem to add my backend/config/main.php file
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => 'false'
],
but it does not work.
Please help me to solve this problem.
Customize URL:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'<_c:[\w\-]+>' => '<_c>/index',
'<_c:[\w\-]+>/<_a:[\w\-]+>' => '<_c>/<_a>',
'<_c:[\w\-]+>/<_a:[\w\-]+>/<id:\d+>' => '<_c>/<_a>',
],
],
In apache.conf input DocumentRoot /var/www/backend/web.