What is the difference between the block controller and the view controller?

Permalink 1 user found helpful
I'm building a new block type with concrete 5.7.5.6 .

In controller.php, if I log the $this object with spl_object_hash from the validate method, I obtain a different value that if I log the $this object with spl_object_hash from the view method.

Is there one block controller and one view controller built from the class in controller.php?

Where can I find information about this?

 
siton replied on at Permalink Reply
siton
I dont understand why inside view or controller you need to use validate method to get "$this" value (i never try this).

About the difference:
C5 pattern for blocks Model-View-Controller (MVC). In short:
Control.php: Control of the block (operations, save, preparing, edit data, and so on).
View.php: Responsible for creating the HTML markup (what the users will see)
model: database.

The most most basic and stupid example:
- Control.php: Charge to take the word in $var "hello-world" and convert the string to "HELLO-WORLD"
- View.php: charge to print the $hello-world var <h1><?php echo $hello_world ?></h1>
///The html output: <h1>HELLO-WORLD</h1>

The idea -
The pattern have split between view (output) and controller (logic,and so on):
Tommorow you change the h1 to span, h2 and so on. Or change the controller to "h-e-l-l-o-w-o-r-l-d".
///The new html output: <span>h-e-l-l-o-w-o-r-l-d</span>

You find a lot of data about blocks in official docs:
http://documentation.concrete5.org/developers/working-with-blocks...

Or in C5 youtube channel (its for 5.6 but the its still helpful):
http://www.concrete5.org/documentation/recorded-trainings/building-...
http://www.concrete5.org/documentation/recorded-trainings/advanced-...

**The MVC pattern its really huge topic (you find books and courses about this)
VonUniGE replied on at Permalink Reply
Hi Siton,

Thanks for your answer and sorry for the delay.

My question was theoric but now I face a practical issue.

I've a block controller with a custom public setTopic($id) method which is called from another block. I then want to pass the $id to the view.

If I do $this->set('selectedTopic', $id); in this method I can't get the value in view.php.

My hypothesis is that the block controller is instantiated two times. In the setThematic() method $this is referring to an object A and in the view() method $this is referring to another object B.

In view.php, both $this and $this->controller are referring to the object B.

So I suppose that the BlockController class defined in controller.php is instantiated one time as a block controller and one time as a view controller.

Can someone confirm or infirm my hypothesis?
siton replied on at Permalink Reply
siton
A. No. One controller and one view. I dont know what do you mean about hypothesis - "2 variables" and why its so matter do you (you work with the var name - one name, and not with var reference pointer like "345345453434" and "23454545656")

B. You cant call methods from another block (you need to import the code "use")

Some MVC functions:
http://legacy-documentation.concrete5.org/developers/blocks/mvc-app...

EXAMPLE:
Start from simple example (not from the topicList - its a little advanced block)

1. Sending variables from the controller to the view":
$controller->set($key, $value)
"Takes a string $key and a mixed $value, and makes a variable of that name available from within a block's view, add or edit template. This is typically used within the add(), edit() or view() function (c5 docs)".

controller.php
public function view() { //view function, will automatically get run when the page loads.
$this->set('my_var_name', 'Hello World'); 
}


Inside view.php
<?php echo $my_var_name ?> //output: Hello World

------------------------------------------------------------------------------------
2. Encapsulation: In the controller you can create custom getters and go "opposite direction" (Get value from controller inside view.php)
//controller.php
private $my_var_name = "john-doe"; // declare 
public function  get_my_var_name(){
    return my_var_name;
}
//view.php
echo $view->controller->get_my_var_name(); //output: john-doe


-----------
Example for this code in C5:
topic list controller.php:
public function getTopicLink(....rest of the code){
}

topic list view.php:
<?php echo $view->controller->getTopicLink($topic); ?>

Or endless get*() functions
VonUniGE replied on at Permalink Reply
A. I'm used to think about objects, when I call a method I like to know precisely on which object I call it.

B . It's definitely possible to call a method on a block from another block. I did it.
Choose a block from the array given by $page->getBlocks() and call the method you want on it.

You will maybe think I'm obstinate, but my problem is still related with the fact that the class defined in controller.php seems to be instantiated two times.

I'm reading and trying the following documentation example :http://documentation.concrete5.org/developers/working-with-blocks/c...

the action method is correctly called, but if I set a private variable in the action method and try to obtain the value with a get method in the view, as you suggested, I obtain only the initial value.

The only explanation I can found is that there is two objects from the BlockController class. The action method is called on one object and the get method called from the view is called on the second object.

I probably did something wrong somewhere but I can't find what.

Thanks for your help.
siton replied on at Permalink Reply
siton
Maybe add your code to the forum and someone will see the problem.
VonUniGE replied on at Permalink Reply
I've a block used to list resources depending on a selected thematic.

In my controller.php
class Controller extends BlockController{
  private $currentThematic = 0;
  public getCurrentThematic(){
     return $this->currentThematic;
  }
  public function action_update($CSRFtoken, $thematicID, $bID){
      $this->currentThematic = $thematicID;
      $this->view();
  }
}

The action_update method is correctly executed and the $thematicID property is correctly set. I've verified it by using logging.

But in view.php, if I do
$view->controller->getCurrentThematic();

I always get the default value 0.

If instead I use in action_update method
$this->set('currentThematic', $currentThematic);

The variable $currentThematic is not set in view.php