How create custom url in yii2

hai all,

how change this url




in yii2

Thank you

By using$defaults-detail:


    'route' => 'dev/index',

    'pattern' => 'custom-own-url', 

    'defaults' => [

        'id' => 2,



Or by using a slug if you need to support multiple URLs.

sorry, I do not know how you mean do "slug to support multiple URLs", But i want this configuration is stored in the database and how I call this(configurasion)of the database to main.php ?

Ah. Then:

  1. Introduce extra field called "slug".

  2. When saving a record either ask for it or generate automatically with

  3. In rules:

'<slug>' => 'dev/index'

  1. In controller:

public function actionIndex($slug)


    $model = MyModel::find()->where(['slug' => $slug])->one();

    // ...


o… i know u mean, thank you so much for all, i like this forum :D

hai me again :D

i have change main urlManager to

'<slug>' => 'dev/index'

and in my view

echo Url::to(['dev/index', 'slug' => '/makan/ayam/berdua-bareng-makan']);

this view print


how make this view print like this


thank you anyway :D

It won’t since parameter is not path so / should be encoded according to URI RFC.