Cannot pass parameter to controller via routing

Hello guys,

I was following the REST tutorial from the wiki and I cannot make passing parameters working.

How shall I configure the "urlManager" rules, so that a parameter is accepted?

If I request via url: domain/controller/method - it works (the method will be called)

domain/controller/method/id - this url isn’t found and I get a 404-error.

My config file contains:


    'components' => [

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'rules' => [

                ['class' => 'yii\rest\UrlRule', 'controller' => 'object'],

            ],

        ],


        'request' => [

            'parsers' => [

                'application/json' => 'yii\web\JsonParser',

            ],

My controller looks that:


<?php


namespace app\controllers;


use yii\rest\ActiveController;

use app\models\Object;


class ObjectController extends ActiveController {


    public $modelClass = 'app\models\Object';


    public function actionView($id)

    {

        return Object::findOne($id);

    }


} 

Please give me a hint for the url manager, so that I can put a (id) parameter in my class method.

Thanks

1 Like

I’d add the “enableStrictParsing” => true just in case.

I’ve used the “extraPatterns” in order to define additional patterns.

But consider that the “view” action is already implemented and available if you’re using an ActiveRecord.

Ideally the REST controller should give you access to the following urls by default:

  • GET objects/<ID>, which will trigger actionView($id)

  • GET objects, triggers actionIndex() that will list all the items

and then the rest of them as per documentation

NOTE: place your additional rules after the REST routes definitions.

Peach, thanks for your answer so far.

Now I’m “quite sure” that I need the “extraPattern” inside my rules, to specify a setting for parameters. But I’m still lost at this point. I’ve played a bit around and my conclusion is:

  • if I specify “enableStrictParsing”, I’m getting the “Page not found.”-message instead of “Unable to resolve the request “object/view/1”.”-message. Also if I call domain/object/view without “enableStrictParsing”, I get the xml-message:

With “enableStrictParsing” I’m getting just “page not found”.

  • I’ve tried the following url rule:

'rules' => [

                ['class' => 'yii\rest\UrlRule',

                    'controller' => 'object',

                    'extraPatterns' => [

                        'view/<id>' => 'view',

                    ],

                ],

            ],

The address domain/object/view/id and domain/object/1 aren’t found that way. I’ve also tried

in the url rule, with the same result. Where am I wrong?

A hint would be greatly appreciated.

again, you shouldn’t need to specify anything in your controller to get the view action working: it should work out of the box via the following url http://yourdomain.com/objects/<ID> : this will trigger the view action and passing the ID automatically.

Just to recap, in config/web.php you just need to have:




    'components' => [

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'enableStrictParsing' => true,

            'rules' => [

                ['class' => 'yii\rest\UrlRule', 'controller' => 'object'],

            ],

        ],



in your controller you just need the following:




<?php


namespace app\controllers;


use yii\rest\ActiveController;

use app\models\Object;


class ObjectController extends ActiveController 

{

    public $modelClass = 'app\models\Object';


} 



and from there you should be able to access /objects to trigger the index action and /objects/<ID> to trigger the view action as I said at the beginning.

Thanks for your answer. The problem was, that I’ve always wrote DOMAIN/object/ID instead of DOMAIN/objects/ID. I’ve missed the plural (objectS). Thanks for your help.

yup found it odd myself at the beginning. I thought that pluralisation could be disabled or controlled some how, still have to figure out how.

I’ve found out, that it works only if


"enableStrictParsing" => false

is set. Otherwise I can only call specific objects. With "false" I can get all objects. In the wiki is written, that by enabled strict parsing, for every url call a method must be set.