Yii2 Rest Custom actions with OPTIONS request - rest

I am working on api up to now it has only been tested in Postman so cors wasnt an issue. However now developing the front end and when it comes to custom Yii actions they are failing the preflight request. I solved this by adding these lines to url-manager config:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/call-rates'],
'pluralize' => false,
'extraPatterns' => [
'OPTIONS' => 'options',
'GET all-resellers' => 'all-resellers',
'POST updatefromcsv' => 'updatefromcsv',
'OPTIONS all-resellers' => 'options',
'OPTIONS updatefromcsv' => 'options',
// other end points....
],
So every custom action has to have a corresponding OPTIONS pattern. I have seen on a similar question that it is possible to apply the OPTIONS pattern to all custom actions in one line like so:
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'v1/call-rates',
// rest of controllers...
],
'pluralize' => false,
'extraPatterns' => [
'OPTIONS <action:\w+>' => 'options',
],
],
However that is not working. I have also tried with a token like this:
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'v1/call-rates'
],
'tokens' => [
'{action}' => '<action:\\w+>',
],
'pluralize' => false,
'extraPatterns' => [
'OPTIONS {action}' => 'options',
],
],
But again no luck. Any help would be appreciated. Thank you in advance.

Solved by adding the following in to urlmanager config:
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'v1/call-rates',
'v1/call-recordings',
],
'tokens' => [
'{action}' => '<action:[a-zA-Z0-9\\-]+>',
],
'pluralize' => false,
'extraPatterns' => [
'OPTIONS {action}' => 'options',
'OPTIONS' => 'options'
],
],
Because my custom actions were named like:
public function actionGetResellers
The Url would be get-resellers so the pattern was incorrect as it didn't accept -'s. So now what whatever controller I add it will add OPTIONS pattern for every custom action

I use Regular Expression <action:\w+-\w+>. Here is the solution:
[
'class' => 'yii\rest\UrlRule', //
'controller' => 'v1/content/my',
'pluralize' => false,
'extraPatterns' => [
'OPTIONS <action:\w+-\w+>' => 'options',
'GET foot-print' => 'foot-print',
'GET my-courses' => 'my-courses',
]

Related

Zend move translator configuration from module.config.php to Module.php

I'm pretty new to ZF and have a question regarding translator configuration. I have an application with the following translator configuration inside the module.cofig file:
'translator' => [
'locale' => 'ru_RU',
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
],
[
'type' => 'phparray',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.php',
],
],
'cache' => \Zend\Cache\StorageFactory::factory(
[
'adapter' => [
'name' => 'Filesystem',
'options' => [
'cache_dir' => APPLICATION_LOAD_PATH . '/data/cache',
'ttl' => '3600',
],
],
'plugins' => [
[
'name' => 'serializer',
'options' => [],
],
'exception_handler' => [
'throw_exceptions' => true,
],
],
]
),
],
This configuration works fine, but I want to know if is it possible to move this code inside Module.php trough the getTranslatorPluginConfig() . What I've tried is to use this method and return this same config:
public function getTranslatorPluginConfig(){
return [
'translator' => [
'locale' => 'ru_RU',
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . '/language',
'pattern' => '%s.mo',
],
[
'type' => 'phparray',
'base_dir' => __DIR__ . '/language',
'pattern' => '%s.php',
],
],
'cache' => \Zend\Cache\StorageFactory::factory(
[
'adapter' => [
'name' => Filesystem::class,
'options' => [
'cache_dir' => APPLICATION_LOAD_PATH . '/data/cache',
'ttl' => '3600',
],
],
'plugins' => [
[
'name' => 'serializer',
'options' => [],
],
'exception_handler' => [
'throw_exceptions' => true,
],
],
]
),
],
];
}
As you can see I haven't changed anything (except base_dir path). I don't get any errors, but the translator is not working at all. If you can tell me what are the steps I need to take to make this configuration work from the Module file and if this is possible at all, I'll be grateful. I don't expect plain code, but just a guidance/suggestion of what could be done, since all I find in the Zend documentation is related with making this configuration inside module.config. Thanks in advance.

Method not allowed(#405)

following code throws out error like this:
"Method Not Allowed (#405)
Method Not Allowed. This url can only handle the following request methods: ."
Any ideas,how to fix this?
['label' => 'Logout', 'url' => ['/site/logout'], 'linkOptions' => ['data' => ['method' => 'post']]],
Here is still method in SiteController:
public function actionLogout() {
Yii::$app->user->logout();
return $this->goHome();
}
use data-method in linkOptions
['label' => 'logOut',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
notice:check behavior in sitecontroller
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout', 'dashboard'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}

rewrite POST method on rest api yii2

I must rewrite post request on rest yii2.
Every time i've post request on url v1/availability
I want to call actionCreate for manage request.
This is my main.php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/availability'],
'pluralize' => true,
'extraPatterns' => [
'POST v1/availability' => 'v1/availability/create'
]
],
'OPTIONS v1/user/login' => 'v1/user/login',
'POST v1/user/login' => 'v1/user/login',
'POST v2/user/login' => 'v2/user/login',
'OPTIONS v2/user/login' => 'v2/user/login',
],
],
In the v1/controllers/AvailabilityController.php
i've
public function actions()
{
$actions = parent::actions();
unset($actions['view']);
return array_merge(
$actions,
[
'index' => [
'class' => 'yii\rest\IndexAction',
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
'prepareDataProvider' => [$this, 'index']
],
]
);
}
public function actionCreate(){
throw new \yii\web\HttpException(200, 'IT WORKS!', 200);
}
Any idea?
add url rule outside like below.
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/availability']
],
'POST v1/availability' => 'v1/availability/create',
'OPTIONS v1/user/login' => 'v1/user/login',
'POST v1/user/login' => 'v1/user/login',
'POST v2/user/login' => 'v2/user/login',
'OPTIONS v2/user/login' => 'v2/user/login',
],
],
And here you need understand rules of Yii. if you add one rule like below.
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
this open many urls for clients.
[
'PUT,PATCH users/<id>' => 'user/update',
'DELETE users/<id>' => 'user/delete',
'GET,HEAD users/<id>' => 'user/view',
'POST users' => 'user/create',
'GET,HEAD users' => 'user/index',
'users/<id>' => 'user/options',
'users' => 'user/options',
]
above all urls will open for clients.
So...
there are 2 way to override post.
First #Irfan Ali method
This method permit to declare single call in main.php and work with single function in controller.
Second method is
in main.php declare general call to availability
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/availability'],
'pluralize' => true,
],
],
],
for intercept post method you must override in the controller
public function createAction($id){
throw new \yii\web\HttpException(200, 'You receive post or put', 200);
}
and in this function you must intercept if call is post you do anything.
I love first method!

ZF2 Route multiple strings to the same controller?

I want to configure my Zf2 application in such a way that multiple strings route to the same controller. For example www.mysite.com/this and www.mysite.com/that both route to the same controller and have this and that available to capture with $this->params. How would I accomplish something like this? Would I need 2 separate route declarations?
'directory' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/string1 || /string2 || /string3',
'defaults' => [
'controller' => 'Application\Controller\MyController',
'action' => 'index'
],
],
]
Easiest solution IMO, is:
'varcatcher' => [
'type' => 'Segment',
'options' => [
'route' => '[/[:tail]]',
'defaults' => [
'controller' => '\Application\Controller\Index',
'action' => 'catch',
'module' => 'Application',
],
'constraints' => [
'tail' => '[a-zA-z0-9_-]*'
],
],
'may_terminate' => true,
],
Then deal with it in your action:
public function catchAction(){
die( $this->params()->fromRoute('tail') );
}
Because ZF2 routes are LIFO. It's probably optimal to deal with it by inserting it first, and handling whatever cases you need to 'catch'.
The mention of LIFO, is because if you define routes 'after' in the router array, those will precede the catch-all, which seems to be of benefit if I've read your question right.
Cheers!
Alex
You could use a Zend\Mvc\Router\Http\Regex route type, instead of a Literal one and do something like
'directory' => [
'type' => 'Zend\Mvc\Router\Http\Regex',
'options' => [
'route' => '/string(?<id>[0-9]+)',
'defaults' => [
'controller' => 'Application\Controller\MyController',
'action' => 'index'
],
],
]
As of definition of Literal route create 3 routes:
'directory1' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/string1',
'defaults' => [
'controller' => 'Application\Controller\MyController',
'action' => 'index',
],
],
],
'directory2' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/string2',
'defaults' => [
'controller' => 'Application\Controller\MyController',
'action' => 'index',
],
],
],
'directory3' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/string3',
'defaults' => [
'controller' => 'Application\Controller\MyController',
'action' => 'index',
],
],
],

Cant use tockens and extrapattern together for REST services in Yii2

Yii2 REST query
I found this for using custom action in the controller for that i added the extrapattern mentioned in the above link
And its working fine when we search .but cant use the normal actions for the controller
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'extraPatterns' => [
'GET search' => 'search'
],
'tokens' => [
'{id}' => '<id:\\w+>'
]
]
],
]
Regards
Thanks all
this solved my problem after lots of trying..
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'extraPatterns' => [
'GET search' => 'search'
],
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'v1/country',
'tokens' => [
'{id}' => '<id:\\w+>'
]
],
],