Implementing Restful Web Service Apis

Hey guys,

I was going to create some functions for a RESTful API and I notice that Yii already has it implemented.

I am editing the web config file inside de config folder to add the example content for the url manager ( under components )

'urlManager' => [

    'enablePrettyUrl' => true,

    'enableStrictParsing' => true,

    'showScriptName' => false,

    'rules' => [

        ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],



The only option that seems to be woring well is the enablePrettyUrl, all the other options break my WebApp. I only have some default models and controllers. Nothing really OUTOFTHEBOX yet.

But is kind of frustrating not knowing why the example does not work.

Anyone is using the RESTful Web Service wihing Yii2.0 ?

Would be great to know how you did it! Thanks for everything !

Docs are a little bit incomplete, so here’s some extra info.

  1. You need to create missing .htaccess file to use pretty urls. This one should be enough:

<IfModule mod_rewrite.c>

    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} -f [OR]

    RewriteCond %{REQUEST_FILENAME} -l [OR]

    RewriteCond %{REQUEST_FILENAME} -d

    RewriteRule ^.*$ - [NC,L]

    RewriteRule ^.*$ index.php [NC,L]


DirectoryIndex index.php

  1. Default restful controllers rely on ActiveRecord, but in basic app template User model extends Object, not ActiveRecord, so you need either to modify this, or use other model.

PS. You can look at 3 files to see how it works:

It’s dead simple.

My bad, it’s already in the docs.

It is perfect man, I did not see it in the guide. I will check it tomorrow and let you know :).

Take care and thanks !

Not working for me ???

I’m using the Basic Template, i created a simple model ‘Category’ within the Gii module, so my model is extending the ActiveRecord Class.

Then i created the CategoriesController extending the ActiveController:

# Content of the file app\controllers\CategoriesController.php


namespace app\controllers;

use yii\rest\ActiveController;

class CategoriesController extends ActiveController


    public $modelClass = 'app\models\Category';


I added a RewriteBase url value to my htaccess file because my webserver’s documentRoot and my webapp are in separate folders:

RewriteEngine on

RewriteBase /~salem/alpha2/web

# If a directory or a file exists, use the request directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise forward the request to index.php

RewriteRule . index.php

My UrlManager configs looks like:

'urlManager' => [

            'enablePrettyUrl' => true,

            'enableStrictParsing' => true,

            'showScriptName' => false,

            'rules' => [

                ['class' => 'yii\rest\UrlRule', 'controller' => 'categories'],



My application just works fine with prettyUrls & without showing ScriptName (always when setting enableStrictParsing to false or adding the related controllers to Roules)

But when acceding to localhost/~salem/alpha2/web/categories i’m having the following error:

Not Found (#404)

Unable to resolve the request "categories/index".

So the application is detecting the CategoriesController Class witch extends the ActiveController Class in witch the indexAction is defined within the ActiveController::actions() and my ActiveRecord model is assigned to the $modelClass property but steal, my app cant find the index action ??

The answer was in the same rest docs:

i desabled the enableStrictParsing, removed Rules, then renamed my controller to CategoryController.

Now! it works!