user2264941
user2264941

Reputation: 407

Route in Yii2 doesn't work

I cant to make request to my controller in Yii2

I have controller /controllers/IndexController.php

class IndexController extends Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionCreateAccount()
    {
        return Json::encode(array('status'=>'ok'));
    }
}

In my config/web.php

    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false
    ],

When I try to make request http://account.ll/Index/CreateAccount I receive an error

Unable to resolve the request "Index/CreateAccount".

When I try to make request http://account.ll/Index I got the same error

Whats wrong?

Upvotes: 0

Views: 780

Answers (3)

user2264941
user2264941

Reputation: 407

Just need to change in url from http://account.ll/Index/CreateAccount to http://account.ll/Index/create-account

Upvotes: 0

xPeng
xPeng

Reputation: 21

Try to change

public function actionCreateAccount()

to

public function actionCreateaccount()

Upvotes: 0

arogachev
arogachev

Reputation: 33548

It should be:

  1. http://account.li/index/index or just http://account.li/index (because index is the default action). If the default controller is IndexController, you can access it like that - http://account.li/.
  2. http://account.li/index/create-account

Controller and action names in actual url should be in lowercase. Action names containing more than one word are transformed with hyphens in between words.

Upvotes: 1

Related Questions