yii2 custom REST api issue - api

I have tried all the way to make these run but no use.There is lots of problem and confusion i m going through.
I have make api which return all the countries which is working fine.Now need to write api function to list all the states of perticular country.
api : http://phpserver:8090/ssn-project/newzit/api/web/state/customstate?country_id=102
StateController.php
class StateController extends ActiveController{
public $modelClass = 'api\modules\state\models\State';
public function actionCustomState($country_id)
{
$model = new $this->modelClass;
$result = $model::find()
->where(['country_id' => $country_id])
->all();
return $result;
}
}
main.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['country/country','state/state','category/category','seller/seller'],
'extraPatterns' => [
'GET CustomState' => 'CustomState',
],
]
],
]
Am I doing anything wrong.Please help

What do you mean by 'controller' => ['country/country','state/state','category/category','seller/seller'] ? This will be treated as module/controller. You have placed all controllers inside different modules? With this logic, your api url will be
http://phpserver:8090/ssn-project/newzit/api/web/state/state/customstate?country_id=102
instead of
http://phpserver:8090/ssn-project/newzit/api/web/state/customstate?country_id=102

Found the solution.
made 'pluralize'=>false and used custom-state in url
My main.php
'rules' => [
[
'pluralize'=>false,
'class' => 'yii\rest\UrlRule',
'controller' => ['country/country','state/state','category/category','seller/seller','contactus/contactus'],
'extraPatterns' => [
'GET custom-state' => 'custom-state',
],
]
],
Thank you.

Related

Yii2 How disable Redis

I made the deply of my application (Yii2) in the production environment of the client and there he uses REDIS in another application (Laravel).
However in my Yii2 application I have nothing using or instantiating REDIS, however when running the error appears below. How do I disable REDIS in the application?
Example web.php
'session' => [
'name' => '_siscoopSessionId',
'savePath' => __DIR__ . '/../runtime',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
UPDATE
My db.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=srv-probat;dbname=siscoop',
'username' => 'intranet',
'password' => '*****',
'charset' => 'utf8',
];
Solution:
Remove all this code:
'session' => [
'name' => '_appnameSessionId',
'savePath' => DIR . '/../runtime',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],

Get full url of current webpage in Yii2

How to get the full URl in address bar to my Yii2 Model using yii\helpers\Url ?
I use $currentUrl = Yii::$app->request->url; but it return site/submit which is defined already in my UrlManager :
'urlManager' => [
'enableStrictParsing' => true,
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/' => 'site/index',
'site/submit' => 'site/submit',
'admin' => 'admin/index',
'admin/login' => 'admin/login',
'admin/index' => 'admin/index',
'admin/logout' => 'admin/logout',
'/<url:.+>' => 'site/index',
'defaultRoute' => 'site/index',
],
],
Best regards,
Yii::$app->request->absoluteUrl
Plz follow this yii2 document
Yii::app()->createAbsoluteUrl(Yii::app()->request->url)

Standalone rbac setup for module yii2

I`ve got two modules in my yii2-basic application. For common users I have "user" table and for superadmin users I have the second "superadmin" table. There are two users with different sessions in my app - users and superadmins. I need to use different, standalone rbac for superadmin users table. Is it possible to setup two different rbacs in two modules? Now it is setup in web.php file globally. Maybe it is possible somehow to setup authManager in module separately, not in global web.php file?
Thanks in advance!
I figured it out! In your module.php file specify authManager with different table names, you are supposed to create them before. You also can specify rbac submodule of mdm\admin if you want to use it as well. Specify there another usertable - SuperAdmin in the code below.
public function init()
{
parent::init();
$this->modules = [
'rbac' => [
'class' => 'mdm\admin\Module',
'controllerMap' => [
'assignment' => [
'class' => 'mdm\admin\controllers\AssignmentController',
'userClassName' => 'app\models\SuperAdmin',
'idField' => 'id',
'usernameField' => 'username',
],
],
'layout' => 'left-menu',
'mainLayout' => '#app/views/layouts/rbac.php',
],
];
$config = [
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
'itemTable' => 'superadmin_auth_item',
'assignmentTable' => 'superadmin_auth_assignment',
'itemChildTable' => 'superadmin_auth_item_child',
'ruleTable' => 'superadmin_auth_rule',
],
],
'as access' => [
'class' => 'app\components\SuperAdminAccessControl',//your overridden AccessControl class
'allowActions' => [
'admin/rbac/*',
'admin/default/login',
'admin/default/logout',
]
],
];
\Yii::$app->authManager->itemTable = 'superadmin_auth_item';
\Yii::$app->authManager->assignmentTable = 'superadmin_auth_assignment';
\Yii::$app->authManager->itemChildTable = 'superadmin_auth_item_child';
\Yii::$app->authManager->ruleTable = 'superadmin_auth_rule';
\Yii::configure(\Yii::$app, $config);
}
Overridden AccessControl class:
namespace app\components;
class SuperAdminAccessControl extends \mdm\admin\components\AccessControl
{
function __construct() {
$this->setUser('superadmin');//table name
}
}

Yii2 as beforeRequest authentication rule in config not working for pages

In a Yii2 advance application, I am trying to allow users to the login page and about page only.
So i put in the /common/config/web.php configuration file the following rule
'as beforeRequest' => [ //if guest user access site so, redirect to login page.
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'actions' => ['login', 'error', 'request-password-reset', 'about'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['#'],
],
],
],
The login, error and request-password-reset it is working fine but not the about page.
I also tried '/page/about' and 'page/about' but i had no luck.
Any ideas how to fix or troubleshoot this.
Thanks
It should be 'view' instead of 'about'
I am sorry I found the answer the rule it is fine but i am using a page component and the PageController it is getting the view from the admin (database) in the following controller
class PageController extends Controller{
public function actionView($slug)
{
$model = Page::find()->where(['slug'=>$slug, 'status'=>Page::STATUS_PUBLISHED])->one();
if (!$model) {
throw new NotFoundHttpException(Yii::t('frontend', 'Page not found'));
}
$viewFile = $model->view ?: 'view';
return $this->render($viewFile, ['model'=>$model]);
}}
So in the rule I need to use 'view' for all the views rendered by the PageController
try this code
'as beforeRequest' => [ //if guest user access site so, redirect to login page.
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'actions' => ['login', 'error', 'request-password-reset', 'about'],
'roles' => ['#']
'allow' => true,
],
[
'allow' => true,
'roles' => ['?'],
],
],
],
i hope it will work for you

Module route with rewrite redirect to product on Prestahop

I created a custom route for an artist page:
public function hookModuleRoutes($params)
{
return [
'module-artists-artist' => [
'controller' => 'artist',
'rule' => 'artists/{id_artist}',
'keywords' => [
'id_artist' => ['regexp' => '[0-9]+', 'param' => 'id_artist'],
],
'params' => [
'fc' => 'module',
'module' => 'artists',
'controller' => 'artist'
],
],
];
}
If I test with /artists/1, this works. But I want to add the link_rewrite property. So I modified the configuration like this:
public function hookModuleRoutes($params)
{
return [
'module-artists-artist' => [
'controller' => 'artist',
'rule' => 'artists/{id_artist}-{rewrite}',
'keywords' => [
'id_artist' => ['regexp' => '[0-9]+', 'param' => 'id_artist'],
'rewrite' => ['regexp' => '[_a-zA-Z0-9\pL\pS-]*'],
],
'params' => [
'fc' => 'module',
'module' => 'artists',
'controller' => 'artist'
],
],
];
}
But when I try /artists/1-baxter, I'm redirected to the product page of product with ID 1. My artist controller is never called.
[Debug] This page has moved
Please use the following URL instead: http://localhost:8000/fr/estampes/1-est-ce-que-etre
How can I solve it?
This is because URLs generated by your pattern also match the product URL pattern, which has higherprecedence. PrestaShop doesn't check whether product exists, it just redirects directly to ProductController. Page patterns in PrestaShop differ from each other so that URLs can be quickly recognized to be associated with X controller. You can confirm this by checking the default patterns.
You can check the product URL pattern in back-office: SEO & URLs or in DispatcherCore class. Anyway, if you want an easy fix I'd suggest making this pattern:
artists/{id_artist}/{rewrite}