I have a Yii2 app. And I try to add the patterns array in the urlManager of web.php. And I serached a lot and fount a lot of links with the correct soltuion. For example:
https://stackoverflow.com/questions/41873686/yii2-apply-custom-url-rule-class
https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing
But It didn’t resolved the issue.
So if I do it like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'class' => 'yiirestUrlRule',
'controller' => 'v1/user',
'POST' => 'v1/user/signup',
'POST' => 'v1/user/login',
],
],
And in postman I do a post on this url: http://localhost:8080/v1/user/login
everything works fine. But if I do this:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'class' => 'yiirestUrlRule',
'controller' => 'v1/user',
'patterns' => [
'POST' => 'signup',
'POST login' => 'login',
'OPTIONS login' => 'options',
],
],
],
the whole web.php file looks:
<?php
// phpcs:ignoreFile
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'name' => 'internetsuite 2.0',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'modules' => [
'v1' => [
'class' => 'appmodulesv1Module',
],
],
'components' => [
'request' => [
'parsers' => [
'application/json' => 'yiiwebJsonParser',
],
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'OEtCunrAfQNETtmUSDnZw1JPHTB44i3A',
],
'cache' => [
'class' => 'yiicachingFileCache',
],
'user' => [
'identityClass' => 'appmodelsUser',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => yiisymfonymailerMailer::class,
'viewPath' => '@app/mail',
// send all mails to a file by default.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yiilogFileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [[
'class' => 'yiirestUrlRule',
'controller' => 'v1/user',
'pluralize' => false,
],
'extraPatterns' => [
'OPTIONS {login}' => 'options',
'POST signup' => 'signup',
'POST login' => 'login',
],
],
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yiidebugModule',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yiigiiModule',
// uncomment the following to add your IP if you are not connecting from localhost.
//'allowedIPs' => ['127.0.0.1', '::1'],
];
}
return $config;
I get this error:
Unknown Property – yiibaseUnknownPropertyException
Setting unknown property: yiiwebUrlRule::POST
And I need the OPTIONS parameter because I call an api call from an react app.
Question: how to implement the pattern in the urlManager correctly?
2
Answers
These rules appear slightly nonsensical, when comparing them to the documentation.
It’s probably more alike this (
<controller>
is just a wildcard used for standard CRUD):The verb
OPTIONS
should be handled with.htaccess
. Setting breakpoints insideUrlManager.php
might support the understanding of these rule-sets. In every case, the documentation seems to be incorrect about not supporting verbOPTIONS
, because line 236 reads:Also see The Definitive Guide to Yii 2.0 – Routing.
I’ve found the problem in your whole
web.php
config code.You put
'extraPatterns'
outside the rest rule configuration so it’s treated as another rule instead of part of rest rule.So correct configuration should look like this:
Side note: I don’t know why you put
login
inside of braces like this{login}
but it doesn’t feel right.OPTIONS login
would match OPTIONS request send to route like thisv1/user/login
. When you put it inside braces you are tellingUrlRule
to replace it with token defined in$tokens
array.