1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't get this to work. I get blank pages like the most people in the comments. I have tried some of those solutions but it doesn't work.
blank page is usually wrong server configurations. In the demo app I linked I had to to add RewriteBase /backend/api to the .htaccess file in order to force it to work in a different environnement (here). a long time ago I had to configure it like this in one more different environnement. check your server configs and be sure mode rewrite is enabled if using apache. the tutorial is working fine with with my current environmt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.