Question:
In a js file (inside public/resources/themes/my_thema/javascript) I make an ajax call according to the jquery standard.
the php file of the call I put it in public/class/myfile.php
Here a call a query to Silverstripe db so to the beggining of the file I put
use Silverstripe/ORM/DB
Details of your query go here
// Include any relevant code. If you have a lot of code, link to a gist instead.
Are you calling that file directly? (eg. http://example.com/class/myfile.php) If so, then youâre bypassing the Silverstripe core code, so things like the class autoloader, etc. wonât work.
If youâre not calling it directly, can you provide a bit more info about where it is in your code base, what the file contains, etc. and we might be able to help.
in public\resources\themes\mytheme\javascript\lista.js
function editTitle() {
var lista_id = getUrlParameter(âidâ);
if (lista_id!=ââ) {
$(â#titololistaâ).blur(function(){
//alert(âaaâ);
$.ajax({
type: âPOSTâ,
url: âpublic/class/test.phpâ,
dataType: âjsonâ,
data: { //dati passati al php
lista_id: lista_id,},
success: function(result) {alert(âokâ);},
async: true
});});
}}
in public\class\test.php
<?php
use SilverStripe\ORM\DB; //in vendor folder
function article($param){
$query="select * from table";
$query=DB::query($query);
......
}
error: Class SilverStripe\ORM\DB non found
OK, so it looks like the JS is making a request directly to the PHP file. My advice would be to move that functionality into the Silverstripe codebase, since you want to be able to gain access to Silverstripe dataobjects, etc. Have a look at the docs link to controllers I posted earlier. Depending on exactly what youâre looking to do, it could be as simple as adding a new method to your PageController class to handle the request.
Iâm sure there is a solution using the structure you currently have⌠you could probably include and bootstrap all the functions you need⌠what I gave you is the best solution as I see it, that should work and retain all the flexibility and security that is provided by default when using the proper framework.
Is there a reason you donât want to do it using the way I suggested?
As @Tim said, it really sounds like youâre approaching this the wrong way (sorry if that sounds offensive) and this will probably make your work really difficult. The components of the Silverstripe framework generally arenât designed to be used discretely (as opposed to a library of independent functions like LoDash) they really assume that the whole request goes through Silverstripe.
BTW, I noticed that your php file is in the public/class/ directory, but the intention of the public folder in Silverstripe 4 is that it should only include static resources such as css and images. Having PHP files in your public directory may be a security risk.