Can I declare a custom function in code section?
I'm trying to declare and call a function in php
code section of a partial but october does not see it.
Here is what the code looks like:
function onStart() {
return sayHi();
}
function sayHi() {
return 'Hi';
}
Would you please tell me what is wrong about this? Are the only allowed functions here those which relate to page life cycle? (onStart(), onEnd(), onInit(), onRun() .. etc)
Answer
It could be helpful sometimes with such questions to include the actual error message that you receive if there is one, so that people don't start guessing wildly in the wrong direction. And in this case there should actually appear an error message like:
ERROR: Call to undefined function sayHi()
The reason for this error here is that the functions in the template's code section are not defined in a global scope, but as methods of some object. So to reference each other, you would have to precede each cross function call inside the section with a $this->
to specify that you are calling a method of the current object, and not a global function.
Changing your ònStart()
function to
function onStart() {
return $this->sayHi();
}
should make it work.
However, you might want to think about what you want to achieve with that return statement. The way the onStart() and other lifecycle functions in October are designed, a return statement is normally not expected. But if it exists, the page execution cycle is halted at that point and the return value is simply sent as a response back to the browser.
If you would simply like to display the message 'Hi' on the page (and before everything else), then you could perhaps better do it like this:
function onStart()
{
$this->sayHi();
}
function sayHi()
{
echo "<h1>Hi!</h1>";
}
so that the page rendering cycle still continues afterwards. But this would still be no good actually, because the "<h1>Hi!</h1>"
is now put out before everything else (that is, before the opening <html>
tag or whatever comes at the top).
I guess you did not actually want to use it that way, other than for functionality testing purposes, but anyway, thought it might be good to explain, so that people get an idea what actually happens there if they stumble across it. ;)
(On a related note, here is some documentation about proper usage for sending a custom response in OctoberCMS.)
(If you'd like to have a dedicated stackechange community for OctoberCMS, plese support the octobercms.stackexchange proposal. I'll remove this annoing message, once that goal is achieved - promise :) )
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?