Search code examples
phpapirestyii2yii2-basic-app

Yii2 UrlManager REST API GET 404 error when giving id parameter


I've made a REST API with the Yii2 basic template. I've made some changes to the webconfig so I could use links like web/users and web/users/1. It works fine but I couldn't access the web/index anymore. So i've added some more rules to the UrlManager.

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            //basic/web
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:[\w\-]+>/<action:[\w\-]+>/<id:[\d]+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',


            //basic/web/users
            ['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'car']],
            //basic/web/countries
            //Id is een string, vandaar de tokens
            ['class' => 'yii\rest\UrlRule', 'controller' => 'country', 'tokens' => ['{id}' => '<id:\\w+>']],
        ],
    ],

With the above code I can acces web/index. I could also access web/users to get a list with users. but I can't acces web/users/1. it gives an 404 error.

edit:

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            //basic/web
            /*
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:[\w\-]+>/<action:[\w\-]+>/<id:[\d]+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            */



            //basic/web/users
            ['class' => 'yii\rest\UrlRule', 'controller' => ['user', 'car', 'site']],
            //basic/web/countries
            //Id is een string, vandaar de tokens
            ['class' => 'yii\rest\UrlRule', 'controller' => 'country', 'tokens' => ['{id}' => '<id:\\w+>']],
        ],
    ],

I've changed it like this. Now I can acces web/sites because of pluralize. But if I go to web/sites/about for example, it will give an 404 error again. So it's not the best solution


Solution

  • I think it won't be easy to maintain a REST API and HTML webpages within the same config file. For example cookies and session usually are disabled with REST as recommended by its stateless nature while you may need both to authenticate a user within a classic HTML page.

    I would recommend modifying your app structure and having your REST API implemented as an independent module with its own routing configuration and maybe its own Entry Script without missing with the web entry.

    A better structure may look like this : (from the tutorial linked below)

    + web
    + config
    + controllers
    ...
    + api
      + config
      + modules
        + v1
          + controllers
      .htaccess
      index.php
    

    Then you'll be able to open your HTML files within web/sites/about while retrieving your resources within api/users/1. Each entry may host its own controllers and own models or they may both share @app/models. Check this step by step tutorial to see how to implement it :

    Creating a REST API for Yii2-basic-template - by Joachim Werner

    You may also check api and auth folders in this app where both are independent REST APIs.