skip to Main Content

I would like to have (use) in my Yii2 application:

  • a standard, SEO-like URLs, with .html at the end (thus 'suffix' => '.html') and
  • API request (basing on very simple code) without this suffix.

So, to make my application being able to serve both http://127.0.0.1/app/site/index.html-like URLs and http://127.0.0.1/uslabs/web/user/2-like API calls.

It this possible? If so, how should I configure urlManager component for this?

I went through “Quick Start” chapter in “RESTful Web Service” section, but it bring no help. They don’t use suffixes in examples given there. I’m stuck with the choice of either one or other scheme.

2

Answers


  1. Chosen as BEST ANSWER

    According to this Yii Forum post, what I'm trying to do is possible, but tricky and not encouraged.

    API calls should be handled by a separate application. At the end of "Quick Start" part for API-like apps in Yii2 guide there's a suggestion:

    While not required, it is recommended that you develop your RESTful APIs as a separate application, different from your Web front end and back end for easier maintenance.


  2. Yii has a REST url router that you can use to associate with certain controllers, like this;

    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'suffix' => 'html',
        'rules' => [
            ['class' => 'yiirestUrlRule',
                'controller' => 'api',
                'suffix' => NULL],
        ],
    ]
    

    See the documentation for more details

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search