1

Please, help me with this problem (I am using Yii, Xampp on a Windows localhost):

So... I've created a new widget in "protected\components\views\Magus.php".

 <?php
   class Magus extends CWidget {

   //////////////////////////////
  public function run()
  {
    echo '<p style="color:blue; ">Trallala :) !</p>'; // do something.
  }
    //////////////////////////////

 } //end class

It's used here ("protected/views/layouts/main.php"):

 <?php
   $this->widget('/views/Magus');
 ?>

And this causes a loader (?) error:

Fatal error: include() [<a href='function.include'>function.include</a>]: 
Cannot redeclare class magus in C:\xampp\htdocs\yii\framework\YiiBase.php on line 421

But the error doesn't happen, if the widget file is placed directly in "protected\components". What is the reason of that error and how to correct it ? Thank you in advance !

1 Answer 1

5

You use wrong paths. I suggest you to put your widget php file into protected/components/Magus.php and then you can call it from view with

$this->widget('application.components.Magus');

Or you can keep it in folder where you have it and call it like this:

$this->widget('application.components.views.Magus');
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your very precise answer Johnatan ! I was aware (sort of) of the more elaborate way of defining loading paths, yet still hoping for a shortcut not requiring the "application.components" part.
If you are auto-loading your components directory via your main config file, you can probably just use the class name and/or you could also use setPathOfAlias and then just use the alias name.

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.