Search code examples
phpresturlyii

Enabling pretty URLs / urlManager completely breaks Yii 2 application runnikg as a REST micro-framework


tl;dr My Yii 2 app, running as a micro-framework, works just fine when using standard ("not pretty") URLs. However when the urlManager application component is enabled in the configuration, the entire application breaks completely and every call to it ends with 404.


Following my other question, I am trying to run a minimalist version (micro framework) of Yii 2 app to act as an RESTful endpoint. I've followed the guide and everything seems to be working when I am calling a standard ("not pretty") URLs:

enter image description here

As suggested in the guide, I have enabled routing / pretty URLs in my application:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => 'post'],
    ],
]

And suddenly my application stops working at all. It serves neither non-pretty:

enter image description here

nor pretty URLs:

enter image description here

It even fails to serve the default controller and action -- site/index.

What am I missing?

It seems that I have everything that I need. Following the guide I have:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

What else must I do in order to make the whole thing work or what am I missing here? Thanks!


Solution

  • This question comes from an misunderstanding. I wasn't aware that when pretty URLs are enabled then yii\rest\UrlRule::$pluralize is set to true by default. Meaning that the request must be:

    http://localhost/micro-app/posts
    

    (plural controller name; notice "s" at the end)

    Not:

    http://localhost/micro-app/post
    

    (singular controller name)

    It is also completely normal that when pretty URLs are enabled then regular URLs doesn't work anymore. That's the reason for getting 404 File Not Found on http://localhost/micro-app/index.php?r=post URL when pretty URLs are enabled.