Doubt with xml and laravel


Good, I'm new to the framework, I have a question about an XML that I'm doing in a project that is laravel, the detail is that I'm doing it entirely with php but when I want to visualize what I've done, I can not . here I leave my code

$html = "";
$url = ""
$xml = simplexml_load_file($url);

for ($i  = 0;$i<10; $i++){
  $id = $xml->entry[$i]->id;
  $publi = $xml->entry[$i]->published;
  $upda = $xml->entry[$i]->updated;
  $title = $xml->entry[$i]->title;
  $content = $xml->entry[$i]->content
  $link =$xml->entry[$i]->link;
  $name = $xml->entry[$i]->author->name;
  $uri =  $xml.->entry[$i]->author->uri;
  $mail =  $xml.->entry[$i]->author->email;
  $html .="<div>
  <div>"update:"$upda &nbsp;&nbsp; "published:"$published </div>

echo $html;

Just add the file to the folder where I am working. I do not know if I have to add some function to the view elsewhere, I have that doubt, thanks in advance.

asked by Juan Romero 25.08.2017 в 00:20

1 answer


The way you are programming it is not correct for the Framework. The minimum you should do is the following:

1. Create a controller with the php code that gets the XML. The drivers are stored in the app/Http/Controllers folder. You can put in your file the name XmlController.php and it would have a code similar to this one:


namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class XmlController extends Controller
    public function show()
        $url = "";
        $xml = simplexml_load_file($url);

        /* Aquí lo mejor es manipular la información de tu XML de acuerdo a lo que se mostrará en la vista */

        return view('xml', ['xmlContent' => $xml]);

2. Create the view that will have the HTML code in charge of displaying the information of the XML This view will be called by the controller. The views are saved in the resources/views/ folder and you will have to name it xml.blade.php . The code would be something like this:

@foreach ($xmlContent as $item)
    <h2>{{ $item->title }}</h2>
        <a><spam>{{ $item->link }}</spam></a>
        <div>update: {{ $item->updated }} published: {{ $item->published }}</div>
        <div>{{ $item->content }}</div>
        <div><spam>{{ $item->name }}</spam></div>
        <div>{{ $item->uri }}</div>
        <div>{{ $item->email }}</div>  

3. Create a route to call the controller when you enter the link you define. The routes are saved in the file routes/web.php . There you can add your new route that the controller will call:

Route::get('/xml', '[email protected]');
answered by 25.08.2017 / 01:21