Search code examples
phpxmllaravelxpathsimplexml

Laravel: simplexml_load_file not working


I'm new to Laravel. I created a Controller-function that reads an xml file in my public (/xml) folder and searches it for a node with a specific id-attribute.

public function show_word($id)
{
    $xml_path = asset('xml/words.xml');

    $xml_sim = simplexml_load_file($xml_path);
    $word_existing = $xml_sim->xpath("//word[@id=" . $id . "]");

    return view('glossarium.vocab_morph', compact('word_existing'));
}

However, when I call the Route with the ID, I get a timeout-error (Maximum execution time of 60 seconds exceeded). My checklist:

  • SimpleXML is enabled in my PHP configuration: Do I need to enable it somehow within Laravel?
  • I tried using backslashes - no success.
  • I cannot return the $xml_sim variable- timeout as well.
  • I can view the xml-file in the browser. Is it possible that laravel cannot access the file somehow?

Can you help me out?


Solution

  • It seems like I used the wrong helper-function. I moved my xml to the laravel resources folder and access the path with the function

    $path = resource_path('xml/words.xml'); 
    

    Thanks nonetheless for your help!