📜 ⬆️ ⬇️

The implementation of components in Kohana

Imagine a situation that we have a widget on our site. This widget is repeated on the site on every page. How do we design the code so that we forget about this widget and focus on other issues?

Of course, we cannot afford to access the data area from the template, otherwise it would indicate our illiteracy. Many freeware have their own solutions for this. But I will draw an analogy with symfony. In the latter there is such a thing as a component. It looks like this: you call a static method with the name of the components in the template, then the controller of the same name is accessed, data is retrieved from the data area and transferred to the template fragment, the last is rendered and the code is inserted into the native template. Thus, the MVC architecture is nested in each other. I simplified this scheme a little, I immediately say, but I conveyed the essence for sure.

So here. There is no ready-made solution in Kohana. That's why I wrote a simple way to implement components, which is solved in three lines of code.
')
So. We write the widget.php helper and throw it into the application / helpers folder. The helper content is as follows:
Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  1. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  2. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  3. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  4. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  5. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  6. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  7. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  8. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  9. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  10. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>
  11. Copy Source | Copy HTML <?php class widget { public static function paste( $className , $functionName , $parameters = null) { $controllerName = $className . '_Controller' ; $controller = new $controllerName ; $controller -> $functionName ( $parameters )->render( true ); } } ?>

In the controller class, we write the usual content method only instead of rendering the template, we return it, that is, it writes return. I always use the slightly overridden Template_Controller controller. An example of a controller method:

Copy Source | Copy HTML
  1. public function widget_example ()
  2. {
  3. return View :: factory ( '_example.php' , array ( 'var' => 'Hello world!' ));
  4. }


Specify the name of the template with an underscore, so as not to be confused.
Now it is enough to write in any template

Copy Source | Copy HTML
  1. <? php widget :: paste ( 'Index' , 'widget_example' ) ?>


And that's all - now instead of this line our widget will be rendered. This is how simple it is. If necessary, we can pass the necessary values ​​to the component as the third argument in the form of an array.
By the way, partial (symfony like) is implemented by simply including it in the template.
Copy Source | Copy HTML
  1. <? php View :: factory ( 'template' ) -> render () ?>

partial is a simple template inclusion (for those who do not know)
PS Just let's not make holivars about Smarty (etc.) vs Native Template.
There are a lot of developments about Kohana - over time, I will publish a series of articles on the blog and here.
UPD. Transfer post to the blog Kohana can not. Not enough karma

Source: https://habr.com/ru/post/55611/


All Articles