Ajout d'un champs "slug" aux producteurs. Ajout systématique du slug dans les urls de l'application "producer".refactoring
Import::using('common\controllers\*'); | Import::using('common\controllers\*'); | ||||
Import::using('frontend\controllers\*'); | Import::using('frontend\controllers\*'); | ||||
Import::using('backend\controllers\*'); | Import::using('backend\controllers\*'); | ||||
Import::using('producer\controllers\*'); | |||||
// helpers | // helpers | ||||
Import::using('common\helpers\*'); | Import::using('common\helpers\*'); | ||||
Import::using('kartik\mpdf\Pdf'); | |||||
// components | |||||
Import::using('common\components\*'); | |||||
Import::using('producer\components\*'); | |||||
Import::using('kartik\mpdf\Pdf'); |
namespace common\helpers; | namespace common\helpers; | ||||
use yii ; | |||||
class Url | class Url | ||||
{ | { | ||||
public static function frontend($action = '') | public static function frontend($action = '') | ||||
} | } | ||||
} | } | ||||
} | } | ||||
public static function slugify($string) { | |||||
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string))); | |||||
} | |||||
} | } |
$this->addError($attribute, 'Ce code est déjà utilisé par un autre producteur.'); | $this->addError($attribute, 'Ce code est déjà utilisé par un autre producteur.'); | ||||
} | } | ||||
}], | }], | ||||
[['description', 'infos_commande'], 'string'], | |||||
[['solde_negatif', 'credit_pain', 'actif'], 'boolean'], | |||||
[['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville', 'code'], 'string', 'max' => 255], | |||||
['prix_libre', 'double'], | |||||
['prix_libre', 'compare', 'compareValue' => 0, 'operator' => '>=', 'type' => 'number', 'message' => 'Prix libre doit être supérieur ou égal à 0'], | |||||
[['description', 'infos_commande','slug'], 'string'], | |||||
[['solde_negatif', 'credit_pain', 'actif'], 'boolean'], | |||||
[['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville', 'code'], 'string', 'max' => 255], | |||||
['prix_libre', 'double'], | |||||
['prix_libre', 'compare', 'compareValue' => 0, 'operator' => '>=', 'type' => 'number', 'message' => 'Prix libre doit être supérieur ou égal à 0'], | |||||
]; | ]; | ||||
} | } | ||||
'credit_pain' => 'Crédit pain', | 'credit_pain' => 'Crédit pain', | ||||
'actif' => 'Actif', | 'actif' => 'Actif', | ||||
'date_creation' => 'Date de création', | 'date_creation' => 'Date de création', | ||||
'infos_commande' => 'Informations' | |||||
'infos_commande' => 'Informations', | |||||
'slug' => 'Slug', | |||||
]; | ]; | ||||
} | } | ||||
<?php | |||||
use yii\db\Migration; | |||||
use yii\db\Schema; | |||||
class m180312_174327_slug_producer extends Migration { | |||||
public function up() { | |||||
$this->addColumn('etablissement','slug',Schema::TYPE_STRING); | |||||
} | |||||
public function down() { | |||||
$this->dropColumn('etablissement','slug'); | |||||
} | |||||
} |
$etablissement->heure_limite_commande = 20; | $etablissement->heure_limite_commande = 20; | ||||
$etablissement->delai_commande = 1; | $etablissement->delai_commande = 1; | ||||
$etablissement->gratuit = 1; | $etablissement->gratuit = 1; | ||||
$etablissement->slug = \common\helpers\Url::slugify($this->nom_magasin) ; | |||||
// génération d'un code | // génération d'un code | ||||
do { | do { |
<?php | |||||
namespace producer\components ; | |||||
class ProducerUrlManager extends \yii\web\UrlManager { | |||||
public function createUrl($params) { | |||||
if(!is_array($params)) | |||||
$params = [$params] ; | |||||
$params['slug_producer'] = Yii::$app->getRequest()->getQueryParam('slug_producer') ; | |||||
return parent::createUrl($params) ; | |||||
} | |||||
} |
var $subtitle ; | var $subtitle ; | ||||
public function getProducer() { | public function getProducer() { | ||||
return Etablissement::findOne(Yii::$app->getRequest()->getQueryParam('id_producer')) ; | |||||
return Etablissement::findOne(['slug' => Yii::$app->getRequest()->getQueryParam('slug_producer')]) ; | |||||
} | } | ||||
public function setTitle($title, $icon = '') { | public function setTitle($title, $icon = '') { |
'class' => 'producer\components\ProducerView', | 'class' => 'producer\components\ProducerView', | ||||
], | ], | ||||
'urlManager' => [ | 'urlManager' => [ | ||||
'class' => 'producer\components\ProducerUrlManager', | |||||
'enablePrettyUrl' => true, | 'enablePrettyUrl' => true, | ||||
'showScriptName' => false, | 'showScriptName' => false, | ||||
'enableStrictParsing' => false, | 'enableStrictParsing' => false, | ||||
'rules' => [ | 'rules' => [ | ||||
'<id_producer:\d+>/<controller>/<action>' => '<controller>/<action>' | |||||
'<slug_producer:\w+>/<controller>/<action>' => '<controller>/<action>', | |||||
], | ], | ||||
], | ], | ||||
], | ], |
<nav id="main-nav"> | <nav id="main-nav"> | ||||
<a id="user" href="#"><span class="glyphicon glyphicon-user"></span> <span class="caret"></span></a> | <a id="user" href="#"><span class="glyphicon glyphicon-user"></span> <span class="caret"></span></a> | ||||
<ul class="nav nav-pills"> | <ul class="nav nav-pills"> | ||||
<li><a href="<?php echo Yii::$app->urlManager->createUrl('producer/index'); ?>"><span class="glyphicon glyphicon-th-large"></span> Accueil</a></li> | |||||
<li><a href="<?php echo Yii::$app->urlManager->createUrl(['producer/index']); ?>"><span class="glyphicon glyphicon-th-large"></span> Accueil</a></li> | |||||
<li><a href="<?php echo Yii::$app->urlManager->createUrl('commande/index'); ?>"><span class="glyphicon glyphicon-plus"></span> Commander</a></li> | <li><a href="<?php echo Yii::$app->urlManager->createUrl('commande/index'); ?>"><span class="glyphicon glyphicon-plus"></span> Commander</a></li> | ||||
<li><a href="<?php echo Yii::$app->urlManager->createUrl('producer/index'); ?>"><span class="glyphicon glyphicon-folder-open"></span> Historique</a></li> | <li><a href="<?php echo Yii::$app->urlManager->createUrl('producer/index'); ?>"><span class="glyphicon glyphicon-folder-open"></span> Historique</a></li> | ||||
<li><a href="<?php echo Yii::$app->urlManager->createUrl('producer/index'); ?>"><span class="glyphicon glyphicon-euro"></span> Crédit</a></li> | <li><a href="<?php echo Yii::$app->urlManager->createUrl('producer/index'); ?>"><span class="glyphicon glyphicon-euro"></span> Crédit</a></li> |