0

I've made an REST API in the Yii2 Basic Template. For this REST API i've changed the web.php. This is what I have added:

'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            //basic/web
            //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+>']],
        ],
    ],

This works fine. But now I can't acces the index page from the web folder anymore. What rule do I need to add to access the pages from the web folder again?

1 Answer 1

1

Try this,

'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' =>    '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ['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+>']],
]
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. I've tried something like this before, but for some reason it works now :)
It gives me a new problem. I can't just get 1 value with the API. For example: web/users gives all the users. but web/user/1 gives an 404 error page
'<controller:\w+>/<action:\w+>' => '<controller>/<action>', This rule is the reason the id's don't work anymore

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.