nostop8 / yii2-rest-api-doc

Simple documentation generator for Yii2 REST applications based on defined API endpoints and actions annotations.
MIT License
21 stars 15 forks source link

What version of jQuery and Bootstrap.css/js is required? #8

Closed Anubarak closed 4 years ago

Anubarak commented 4 years ago

Can you please tell me what versions of Bootstrap and jQuery you require? The Bootstrapplugin is not required in your composerand I don't know what version you used, I tried 4.x and 3.x and Bootstrap 3.x seems to work, but I always receive a JS error when clicking on an item

Uncaught Error: Syntax error, unrecognized expression: #xxxx

It looks like this currently image

nostop8 commented 4 years ago

hi @Anubarak

In deed there was no dependency set in composer.json for bootstrap. I have added it now. Thanks! For testing purpose I did following:

  1. backup just in case vendor folder and composer.lock file
  2. removed following folders:
    • vendor/bower-asset/bootstrap
    • vendor/bower-asset/jquery
    • vendor/yiisoft/yii2-bootstrap
    • vendor/nostop8/rest-api-doc
      1. Inside your project composer.json make sure that required version of module is set to * ("nostop8/rest-api-doc": "*")
      2. Run composer install in your project

This is the result I got after with modules versions below: image

It works without javascript errors after this for me. In case after this it still provides an error for you, the problem might be somewhere else. E.g. some other module conflicts with my, overrides the jquery or bootstrap js files.

Anubarak commented 4 years ago

Thank you very much for your fast reply. I was able to find the JS issue. I use sub-modules for the versioning of my API and your plugin only picked the controller name, for example v1/foo then you included that as an ID of the HTML element but slashes in IDs are forbidden so $('#v1/foo') throws an error.

I fixed it via

    public function actionIndex()
    {
        $rules = [];
        foreach (\Yii::$app->urlManager->rules as $urlRule) {
            if ($urlRule instanceof \yii\rest\UrlRule) {
                $entity = [];
                $urlName = key($urlRule->controller);
                $controllerName = current($urlRule->controller);
                $entity['title'] = ucfirst($controllerName);

                // convert slashes, just a hotfix for my situation, usually you would rely on correct regex replacement
                $entity['id'] = str_replace('/', '-', ucfirst($controllerName));

                $urlRuleReflection = new \ReflectionClass($urlRule);
                $rulesObject = $urlRuleReflection->getProperty('rules');
                $rulesObject->setAccessible(true);
                $generatedRules = $rulesObject->getValue($urlRule);

                $entity['rules'] = $this->_processRules($generatedRules[$urlName]);

                $rules[] = $entity;
            }
        }

        return $this->render('index', [
                'rules' => $rules,
        ]);
    }

Thanks again, it can be closed