Refilon
Refilon

Reputation: 3489

return class in template file yii

I'm new to Yii, and i am struggling with the display of some classes. I have a very basic example:

I got my template file: themes/classic/views/layout/main.php and i want to call a class there.

<div class="col-lg-2 col-md-2 col-sm-2">
    <div class="box">
        <h2>Categorieën</h2>
        <p>
            <?php
            echo AdminController::producten(); //<--- this does not work!
            ?>
        </p>
    </div><!-- box -->
</div><!-- col-lg-2 -->

The class is protected/controllers/AdminController.php

In that class i have a function called producten()

public function producten(){
    return 'Hier komen categorieën!';
}

I want to view that function (in this case a string) in my template page.

Can you guys show me how, if this is possible ofcourse. Or should i use a widget?

Upvotes: 2

Views: 60

Answers (1)

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

In order to make below code working:

echo AdminController::producten();

You need to change your method to static method like below:

public static function producten(){
    return 'Hier komen categorieën!';
}

On the other hand, if your controller is extended from a base controller which holds your main layout, you can use $this keyword to get a method.

When you install a fresh yii web application base controller is under /protected/components/Controller.php which all controllers will extend that. So if you put your method in that Controller, it will be accessible via all controller which are extended from base controller.

Upvotes: 1

Related Questions