0

Coming from laravel background. Currently developing web service using yii framework but routing is not working as expected.

This is the expected route to call web service. "api" is ApiController, "totalviews" is actionTotalviews.

  localhost/myproject/admin/api/totalviews

This is my web.php file in config directory.

`'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'homeUrl' => 'http://localhost/myproject/admin/', 
    'bootstrap' => ['log'],
    'defaultRoute' => 'login/index',
    'components' => [
        'twiliosms' => [
            'class' => 'app\components\TwilioSms',
        ],
        'request' => [
            'cookieValidationKey' => 'abc123',
            'parsers' => [
                'application/json' => 'yii\web\JsonParser',
            ]
        ],
        'urlManager' =>
            [
                'class' => 'yii\web\UrlManager',
                'showScriptName' => false,
                'enablePrettyUrl' => true,
                'rules' => [
                    '<controller:\w+>/<id:\d+>' => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                    '<view:(about)>' => 'login/page',
                    '<action:(index)>' => 'login/<action>',
                    '<action:(forgotpassword)>' => 'login/forgotpassword<action>',
                    '<alias:dashboard|forgotpassword>' => 'login/<alias>',
                ],
            ]

The is htaccess file.

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

This is admin/index.php.

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();`

The default route will direct to login controller and actionIndex. But I tried the below to expected route but 404 not found.

   -localhost/myproject/admin/api/totalviews
   -localhost/myproject/admin/index.php?r=api/totalviews
   -localhost/myproject/index.php?r=api/totalviews

Appreciate your assistance!

6
  • where is your entry script (index.php) in your project? if you are using the default yii2 basic template or advanced template, the entry script should be inside the web directory inside your application. Commented Jun 6, 2017 at 15:59
  • @marche Hi, I edited the question and put in index.php. Commented Jun 7, 2017 at 0:33
  • wich yii2 you use? yii2advanced or yii2basic? Commented Jun 7, 2017 at 5:32
  • It is yii2 basic. @ali Commented Jun 7, 2017 at 6:02
  • why you use admin in your rout? Are you test without admin? Commented Jun 13, 2017 at 4:32

0

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.