官术网_书友最值得收藏!

  • concrete5 Cookbook
  • David Strack
  • 458字
  • 2021-08-13 16:16:00

Using block controller callback functions

The block controller class contains a couple of special functions that get automatically called at different points throughout the page load process. You can look into these callbacks to power different functionalities of your block type.

Getting ready

To get started, you will need a block type created and installed. See the previous recipe for a lesson on creating a custom block type. We will be adding some methods to controller.php.

How to do it...

The steps for using block controller callback functions are as follows:

  1. Open your block's controller.php file.
  2. Add a new function called on_start():
    public function on_start() {
    }
  3. Write a die statement that will get fired when the controller is loaded.
    die('hello world');
  4. Refresh any page containing the block type. The page should stop rendering before it is complete with your debug message.
  5. Be sure to remove the die statement, otherwise your block won't work anymore!

How it works...

concrete5 will call the various callback functions at different points during the page load process. The on_start() function is the first to get called. It is a good place to put things that you want to happen before the block is rendered.

The next function that gets called depends on how you are interacting with the block. If you are just viewing it on a page, the view() function gets called. If you are adding or editing the block, then the add() or edit() functions will get called as appropriate. These functions are a good place to send variables to the view, which we will show how to do in the next recipe. The save() and delete() functions also get called automatically at this point, if the block is performing either of those functions.

After that, concrete5 will call the on_before_render() function. This is a good time to add items to the page header and footer, since it is before concrete5 renders the HTML for the page. We will be doing this later on in the chapter.

Finally, the on_page_view() function is called. This is actually run once the page is being rendered, so it is the last place where you have the code executed in your block controller. This is helpful when adding HTML items to the page.

There's more...

The following functions can be added to your controller class and they will get called automatically at different points throughout the block's loading process.

  • on_start
  • on_before_render
  • view
  • add
  • edit
  • on_page_view
  • save
  • delete

For a complete list of the callback functions available, check out the source for the block controller library, located in /concrete/core/libraries/block_controller.php.

See also

  • The Sending variables from the controller to the view recipe
  • The Adding items to the page header and footer from the block controller recipe
主站蜘蛛池模板: 长白| 北流市| 龙里县| 于田县| 广昌县| 祁阳县| 丁青县| 宁南县| 辽源市| 福清市| 洛宁县| 宾阳县| 泸水县| 勐海县| 岚皋县| 都兰县| 泊头市| 游戏| 剑河县| 东台市| 高安市| 宁德市| 仁怀市| 道孚县| 正阳县| 稷山县| 广南县| 东阿县| 宝坻区| 那坡县| 鄂托克前旗| 石楼县| 古蔺县| 砀山县| 新龙县| 邹平县| 石景山区| 临西县| 屏南县| 措美县| 怀柔区|