PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Object not found when accessing yii2 controller methods

 January 23, 2022     php, yii, yii2, yii2-user     No comments   

Issue

Hi I'm a new bie to YII 2 framework,

I'm currently learning from following tutorial http://www.yiiframework.com/wiki/490/creating-a-simple-crud-app-with-yii2-revised-12-20-2013/

Everything worked well but when I created a function within SiteController.php

i.e

 public function actionLogin()
    {

        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

and when I access it from the browser as follow,

http://localhost/basic/web/site/login/

I'm getting

Object not found! in my browser but I can access the SiteController.php index function as follow http://localhost/basic/web/

Not sure what I'm missing here, could you please let me know the issue?

Thanks in Adavance

EDIT : For debug purpose I placed die statement in the \basic\web\index.php apparently it's not hitting that file also


Solution

Ok. I understand. You not use .htaccess. Please put this .htaccess in web folder. And you need check to Apache modue mod_rewrite is avaible now.

#Options +FollowSymLinks
#IndexIgnore */*

#RewriteEngine on

# if a directory or a file exists, use it directly
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
#RewriteRule . index.php


    # use mod_rewrite for pretty URL support
    RewriteEngine on
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php

    # ...other settings...

See more in https://github.com/yiisoft/yii2/blob/master/docs/guide/start-installation.md#recommended-apache-configuration-

And urlManager in components like that https://yadi.sk/i/TIKuhYPHehMJq

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,          
            'rules' => [
                '<_c:[\w\-]+>' => '<_c>/index',
                '<_c:[\w\-]+>/<_a:[\w\-]+>' => '<_c>/<_a>',
                '<_c:[\w\-]+>/<_a:[\w\-]+>/<id:\d+>' => '<_c>/<_a>',
            ],
        ],

It is work - https://yadi.sk/i/7iOzHBm1ehMFE



Answered By - vitalik_74
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing