Comments (6)
Hi,
I apologize for the delay, and I hope this will still help.
You can define a callback which will add a default response if none is given in your user functions.
https://www.jaxon-php.org/docs/responses/callbacks.html
jaxon()->register(Jaxon::PROCESSING_EVENT, Jaxon::PROCESSING_EVENT_AFTER, 'setDefaultResponse');
function setDefaultResponse()
{
$response = jaxon()->newResponse();
// You can use the global response provided by the library
// $response = jaxon()->getResponse();
// Set the default response content
...
// Append your response to the request response
jaxon()->getResponseManager()->append($response);
}
from jaxon-core.
Thanks for the suggestion. I added that but the fatal error happens before it gets to running setDefaultResponse. If I try PROCESSING_EVENT_BEFORE it gets there, but seems to make no difference in the fatal error above.
Also just to be clear I don't know ahead of time if the function has a response returned since I'm trying to make a backward compatible replacement that just works without touching a bunch of sites.
Likewise, I wouldn't want to do something that just swapped not needing to specify a response object by default with an approach that overrode when there WAS a response returned, if that's where this is headed.
$this->ajax_class = new \Jaxon\Jaxon();
$this->ajax_class->setOption("core.prefix.function","xajax_");
$this->ajax_class->setOption("core.request.uri",$_SERVER["REQUEST_URI"]);
//the line below didn't exist before your suggestion
$this->ajax_class->register(\Jaxon\Jaxon::PROCESSING_EVENT, \Jaxon\Jaxon::PROCESSING_EVENT_BEFORE, 'setDefaultResponse');
foreach($this->functions as $f)
{
$this->ajax_class->register(\Jaxon\Jaxon::USER_FUNCTION, $f);
}
$this->ajax_class->processRequest();
and then as an example of what $this->functions could contain, here would be one with no response
function save_sorting($array)
{
$index = 1;
foreach($array as $id)
{
$temp = new Page($id);
$menu = $temp->getMenu();
if($menu->exists())
{
$menu->setSortOrder($index);
}
$index++;
}
}
This results in a fatal error. However when I return a new Jaxon\Response\Response in save_sorting all is well.
Of course I could be missing the boat completely on something.
from jaxon-core.
Hi,
You were right, until the latest https://github.com/jaxon-php/jaxon-core/releases/tag/v2.2.4 release I just shipped, it was not possible in a user function not to return a response.
With this release, you can define your event handler as follow.
function setDefaultResponse()
{
$manager = jaxon()->getResponseManager();
if($manager->hasNoResponse())
{
$response = jaxon()->newResponse();
// You can use the global response provided by the library
// $response = jaxon()->getResponse();
// Set the default response content
...
// Append your response to the request response
$manager->append($response);
}
from jaxon-core.
Awesome, thanks. Do we have to set a default, or is just doing nothing nothing cool too?
from jaxon-core.
You're welcome.
You have to set a default, since the request actually needs a response to be returned to the client.
from jaxon-core.
Do you think it is a good idea if Jaxon returns the global response object when no response is defined either in the user function or in the after processing callback?
from jaxon-core.
Related Issues (20)
- replace funciton not working HOT 3
- getClass deprecated in Di\Container.php HOT 3
- Domain for Documentation seems down HOT 1
- Call to undefined function Jaxon\CodeIgniter\Filter\jaxon() HOT 2
- Jaxon-Upload Help
- CSP if SSL is in use...
- Examples links are returning an Error HOT 4
- Reinstate CallableObjects in version 4 possible? HOT 2
- PHP 8.2 warning: Deprecated: Creation of dynamic property Jaxon\Utils\Template\Context is deprecated HOT 2
- work with swoole framwork HOT 3
- Problems running most jaxon examples HOT 1
- Uncaught TypeError: Jaxon\Plugin\Request\CallableFunction\CallableFunction::configure(): Argument #2 ($sValue) must be of type string HOT 1
- Paramter as string '0' HOT 1
- Going from V2.x to V4.x HOT 6
- Stuck on 3.8.2; will not update to latest
- myJaxon extends Jaxon || myResponse extends Response HOT 10
- Register CALLABLE_CLASS problem HOT 6
- Jaxon with single class file per web page HOT 7
- Jaxon and the factory pattern HOT 21
- Directory Registration HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jaxon-core.