0

I am trying to collaborate Nginx and Yii. I have set nginx root directory as the yii webapp as - yiic webapp /usr/share/nginx/app

In this directory I have some number of default files like yii's index.php, index-test.php along with the essential folders like protected, themes, css and images. Also I have my own files; phpinfo.php to print phpinfo() and getAttribute.php to print some columns from the mysql table history. I am able to display the phpinfo when I hit http://localhost/phpinfo.php but I can't show the output of getAttribute.php-

#getAttribute.php
<?php
public function attributeLabels() {
return array(
    Yii::t('app','model.history.sfExternalfield')=>array(
            'External Field'=>Yii::t('app','model.history.sfExternalfield'),
            'Delivery Status'=>Yii::t('app','model.history.deliveryStatus'),
    )
);
}
?>
<html>
<body><?php
print_r(attributeLabels()); 
?></body>
</html>
<?php ?>

Is there something wrong with this code?

3
  • 1
    first thing you can't have the public modifier here without having the function within a class. Second, tell me where is this file? in the root folder, along with index.php etc, or within a subfolder? Commented Oct 3, 2012 at 14:25
  • @bool.dev, Thanks for replying. I'll remove public and check it out. And yes, this file is in the root folder, along with the index.php and other default files. Commented Oct 4, 2012 at 5:12
  • @bool.dev, I remvoed - public. The page loads in the browser but it is blank. Commented Oct 4, 2012 at 5:15

1 Answer 1

1

There are two things wrong with the code:

  1. As i mentioned in the comment, you can't have the public keyword without a class, so you have to remove that first.

  2. Secondly since this file is not being accessed through the index.php, but directly, it means that the framework is not yet loaded/initialized. So you don't have access to the Yii class yet. To do that you'll have to include the Yii class, somewhat like this:

    $yii='path/to/framework/yii.php';
    require_once($yii);
    // now Yii is available and you can call Yii::t();
    
Sign up to request clarification or add additional context in comments.

5 Comments

my framework folder is at /Documents/yii-1.1.12.b600af/framework. When I set $yii variable to this path, it doesn't work. I get Server Error in google-chrome. Should I set $yii to /Documents/yii-1.1.12.b600af/framework/yiic.php or some other file?
Updated $yii to the path to yii.php which is /Documents/yii-1.1.12.b600af/framework/yii.php and it seems to work. It gives Array ( [model.history.sfExternalfield] => Array ( [External Field] => model.history.sfExternalfield [Delivery Status] => model.history.deliveryStatus ) ) in the browser, but not the actual data from history model. Thanks anyway man.
model - "history" created by model and crud commands from yiic prompt works fine in the framework when I hit - http://localhost/index.php?r=history. I think, maybe I should have an - MVC pattern for my getAttribute.php
yes you are right, about going for an mvc pattern, is the way. if you tell me what is your actual goal of this file i could probably give you more details.

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.