ZF2 cannot match dynamic wildcard route for breadcrumb - php

My system has global dynamic routes which allow to develop modules using the same code style.
I want to generate breadcrumb for url like this /checkout/list/cart-type/2 but navigation config cannot match my url.
On the other hand, when I simply route to /checkout/list it works correctly.
Please, help me to configure my config properly.
My router config
'router' => [
'routes' => [
'default' => [
'type' => 'Segment',
'options' => [
'route' => '/[:controller[/[:action]]]', // global route
'constraints' => [
'controller' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard',
'priority' => 10,
'options' => [],
],
],
],
],
],
My navigation config
'navigation' => [
'default' => [
'checkout' => [
'module' => 'checkout',
'label' => 'Home',
'route' => 'default',
'controller' => 'index',
'action' => 'index',
'pages' => [
'checkout-list' => [
'label' => 'Invoices',
'route' => 'default/wildcard',
'controller' => 'checkout',
'action' => 'list',
'params' => [
'cart-type' => 2
],
],
],
],
],
],

You defined controller and action as parameters, so try (not tested):
'navigation' => [
'default' => [
'checkout' => [
'module' => 'checkout',
'label' => 'Home',
'route' => 'default',
'controller' => 'index',
'action' => 'index',
'pages' => [
'checkout-list' => [
'label' => 'Invoices',
'route' => 'default/wildcard',
'params' => [
'controller' => 'checkout',
'action' => 'list',
'cart-type' => 2
],
],
],
],
],
],
Or:
$url('default/wildcard', [
'controller' => 'checkout',
'action' => 'list',
'cart-type' => 2
];

I found out the solution.
Problem was in 'id' child Segment route which re defined wildcard route
'default' => [
'type' => 'Segment',
'options' => [
'route' => '/[:controller[/[:action]]]', // global route
'constraints' => [
'controller' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
'action' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
],
'defaults' => [
'controller' => 'index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'id' => [
'type' => 'Segment',
'priority' => 100,
'options' => [
//'route' => '[/:id]', // this was changed without brackets (!)
'route' => '/:id',
'constraints' => [
'id' => '[0-9]+',
],
'defaults' => [
'id' => '0',
],
],
'may_terminate' => true,
'child_routes' => [
'wildcard' => [
'type' => 'Wildcard',
'options' => [],
],
],
],
]
]

Related

Migration - Failed to instantiate component or class "db" in console Yii2

I am new to Yii2 and need to set up an existing project. I am at the stage where I would like to migrate tables to the database using the console. I have a database connection in Yii, everything in config is specified correctly. However, I get an error in the console when I try to migrate the tables:
Exception 'yiibaseInvalidConfigException' with message 'Failed to instantiate component or class "db".'
I have already searched Google and have not found a solution to this problem.
Here some code:
config/console.php
<?php
use kartik\mpdf\Pdf;
Yii::setAlias('#webdir', realpath(dirname(__FILE__) . '/../../'));
$params = require(__DIR__ . '/params.php');
$db = require(__DIR__ . '/db.php');
$config = [
'id' => 'easyads-console',
'basePath' => dirname(__DIR__),
'aliases' => [
'#assets' => realpath('../assets'),
'#webroot' => realpath('../'),
],
'bootstrap' => [
'log',
'app\yii\base\Settings',
],
'on beforeRequest' => ['\app\init\Application', 'consoleBeforeRequest'],
'controllerNamespace' => 'app\commands',
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Module',
'defaultRoute' => 'admin',
],
],
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'options' => [
'class' => '\twisted1919\options\Options'
],
'pdf' => [
'mode' => Pdf::MODE_UTF8,
'class' => Pdf::classname(),
'format' => Pdf::FORMAT_A4,
'orientation' => Pdf::ORIENT_PORTRAIT,
'destination' => Pdf::DEST_BROWSER,
],
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
[
'pattern' => 'listing/index/<slug:[a-z0-9_\-]+>',
'route' => 'listing/index',
]
]
],
'generateInvoicePdf' => [
'class' => 'app\components\GenerateInvoicePdfComponent',
],
'sendInvoice' => [
'class' => 'app\components\SendInvoiceComponent',
],
'mailer' => [
'class' => 'app\yii\swiftmailer\Mailer',
],
'consoleRunner' => [
'class' => 'vova07\console\ConsoleRunner',
'file' => '#app/console.php'
],
'mailQueue' => [
'class' => 'app\components\mail\queue\MailQueueComponent',
],
'twigTemplate' => [
'class' => 'app\components\mail\template\MailTemplateComponent',
],
'mailSystem' => [
'class' => 'app\components\mail\MailSystemComponent',
],
'migration' => [
'class' => 'twisted1919\helpers\Migration',
],
'mutex' => [
'class' => 'yii\mutex\FileMutex',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
if (is_file($file = __DIR__ . "/console-local.php")) {
$config = \yii\helpers\ArrayHelper::merge($config, require $file);
}
return $config;
config/db.php
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=database;port=3306;dbname=db',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'tablePrefix'=> 'ea_',
'on afterOpen' => function($event) {
$event->sender->createCommand('SET time_zone="+00:00"')->execute();
$event->sender->createCommand('SET NAMES utf8')->execute();
$event->sender->createCommand('SET SQL_MODE=""')->execute();
},
];

Turning on PageCache in yii2 produces strange encoding problem

I have a web portal that I want to add PageCache to. When Activating it, a strange coding appears and after debugging for a while I don't understand the reason since in other projects developed in Yii2 the PageCache works correctly.
[
'class' => 'yii\filters\PageCache',
'only' => ['index'],
'duration' => 300,
'enabled'=>true,
'variations' => [
\Yii::$app->language,
],
'dependency' => [
'class' => 'yii\caching\DbDependency',
'sql' => 'SELECT COUNT(*) FROM item',
],
],
Web config
$config = [
'id' => 'my-web',
'language'=>'en',
'name'=>'My Web',
'basePath' => dirname(__DIR__),
'defaultRoute' => 'site/index',
'bootstrap' => ['log'],
'aliases' => [
'#bower' => '#vendor/bower-asset',
'#npm' => '#vendor/npm-asset',
],
'components' => [
'request' => [
'cookieValidationKey' => 'XXXXXXXXXXXXXX',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'identityCookie' => ['name' => '_cookiename', 'httpOnly' => true],
],
'session' => [
'name' => 'session-identifier',
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
],
'params' => $params,
];
Ubuntu: Ubuntu 16.04.2 LTS
Mysql: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)
php: 7.0.18
Cache content snippet
^_<8b>^H^#^#^#^#^#^#^Cí=ÛvÛ6¶ïý
X<9d><99>$§¦Dêfɱ<9d>ºN<9c>fN.<9e>ÆM<93>^V^VDB^Rl<92>` Ò<96>ÓéÇôq^^æá¬y<9b><97>®ÕüØÙ^#x'%Q<8a>SÛ­<95>^UY$.^[ØØwÜv6^^¾88~sô^HM^CÇÞûdGüA6v'»^MÂ^[{<9f> øìL ¶ÔOùè<90>^##s<8a>}N<82>ÝÆ×Ç<87>Ú Ñ*¦»Ø!»<8d>3JÎ=æ^G^Md27 .ä?§V0ݵÈ^Y5<89>&^_6^Qui#±­q^SÛd×ØD^N<9e>Q't¢^W½¦¾<89>BN|ù<8c>GðJo¤^#+!<9b>Ü^_k^^ö±<93><81>ýV¼<85><92>¥<8c>^A;%n&ã^[ûô5?^\¿èøüõÁ´£=yr|pö<90><85>m^]^?«^]q|ôz<82>ñ<81>ñTÿö^_úQçéøY<97>¾~wñ¤û^Of|^[|K<9f>^?ó<9a>¾6IçèâÕ³ÿýû^E^?F¦ôéì|w7Óê<9d><80>^F6Ù{é`?xÆΨ<8d>4ô^U^Q-6éûÿ¸( öû^?<8d><99>Ë8rÞÿ^GÒ ßD¾Ìà#z4e.<81>^R/^A^_^NvM¼ÓRõ¥õÛÔ=<85>^B6t^Q»Ì¥<80>º^F<9a>úd¼Û<98>^F<81>·Ýj^YÃvÓè^O<9a><9d>NÓÐ[<8d>ʲØ^N<88>ïâ<80>¨² i,¬ Í!^G^\^FSægð<9c>ö~ÙxZ<84><9b>>õ^Bʲã^Tã£^BY<88>Ǩi&xe.r<98>J^M^D^Y^E<80>RA<9e>ï^? äË^Kä<84>æ<94>A^]¿ð&:^#(Ø<81>*^A^\^Bê^C<8a>¥^LY$^Z^E5Ldæù<84>ó^XF<4<8d>j^DL^X<9b>ØDã4 Ú^Yñé^XF¥Ð¥Ù<9b>Wçmã^[»ëôÂCþhöôHcìø^_o»Ý§£·§^G§Ó·xüÎÖ^O¼Çæ\<;ü^LÛÔ<82>QkêF¦î~ça»ß3<8c>n{h|qð¨×ë^_^ZíÃîðàáaGï^Z<83>\<85><85><9a>=<9f>yÄ^O.v^[l²m3Á<98><99><9a ......
I added several screenshots
What can be the reason for this behavior?

Receiving: "Route with name "imageAdd" not found" for defined route

I am receiving this error in Zend3 for a route that is defined in my module.config.php file where my routes are defined.
Here is the link on my view file that is causing the error:
<p>
Add Image
</p>
Here is my router set up:
'router' => [
'routes' => [
'property' => [
'type' => Segment::class,
'options' => [
'route' => '/property',
'defaults' => [
'controller' => Controller\ListController::class,
'action' => 'forsale',
],
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
'may_terminate' => true,
'child_routes' => [
'detail' => [
'type' => Segment::class,
'options' => [
'route' => '/detail[/:p_id]',
'defaults' => [
'action' => 'detail',
],
'constraints' => [
'p_id' => '[1-9]\d*',
],
],
],
'add' => [
'type' => Literal::class,
'options' => [
'route' => '/add',
'defaults' => [
'controller' => Controller\WriteController::class,
'action' => 'add',
],
],
],
'imageAdd' => [
'type' => Segment::class,
'options' => [
'route' => '/imageAdd[/:p_id]',
'defaults' => [
'controller' => Controller\WriteController::class,
'action' => 'imageAdd',
],
'constraints' => [
'p_id' => '[1-9]\d*',
],
],
],
],
],
],
],
Any help you can provide would be most appreciated, I look forward to hearing from you.
You should simply define your parent "property" route as a Literal type and not a Segment type. That should suffice to enable access to the child routes.

Child routes are not working

I'm new to Zend-Framework3.
And migrating my ZF2 application to ZF3.
In this child routes are not working.
Here is router from my module.config.php
'router' => [
'routes' => [
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'kk' => [
'type' => Literal::class,
'options' => [
'route' => 'kk',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'kk'
],
],
],
]
]
],
],
When I try to call /application/kk action. It generates 404 error.
Where am I wrong? Or do I have to register all actions manually?
...do I have to register all actions manually?
No, you are just missing / character in route value
'router' => [
'routes' => [
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'kk' => [
'type' => Literal::class,
'options' => [
'route' => '/kk', <-- here
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'kk'
],
],
],
]
]
],
],
As long as action kk exists, you should not get 404 error.
If your routes are same as actions name. You can use Segment type:
'application' => [
'type' => Segment::class,
'options' => [
'route' => '/application[/:action]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
]

CakePHP3: how to use different authentication mechanisms?

I have a standard Form authentication declared in AppController.php:
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'scope' => ['Users.active' => 1]
]
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'account'
],
'logoutRedirect' => [
'controller' => 'Index',
'action' => 'index'
]
]);
Now I want an authentication based on api_key in a webservice. The doc, explains to do it like that:
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
So now I wonder how to load the second authentication mechanism in my webservice. I tried to do that:
class DeviceconnectionsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Auth', [
'authenticate' => [
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
}
....
}
But Cake complains that I try to reload a different Auth Component.
So maybe the right way is to load both authentication mechanisms in AppController.php like below:
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'authenticate' => [
'Form' => [
'scope' => ['Users.active' => 1]
],
'Basic' => [
'fields' => ['username' => 'username', 'password' => 'api_key'],
'userModel' => 'Users'
],
],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'account'
],
'logoutRedirect' => [
'controller' => 'Index',
'action' => 'index'
]
]);
But it seems incorrect as both authentication uses a different storage and unauthorizedRedirect setting.
How to do?

Categories