Doubt with xml and laravel

2

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

    <?php
$html = "";
$url = "http://feeds.feedburner.com/blogspot/amDG?format=xml"
$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>
  <h1>$title</h1>
  <h3>
  <a><spam>$link</spam></a>
  <div>"update:"$upda &nbsp;&nbsp; "published:"$published </div>
  <br>
  <div>$content</div>
  <br>
  <div><spam>$name</spam></div>
  <br>
  <div>$uri</div>
  <div>$mail</div>  
  </h3>

  </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
source

1 answer

1

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:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class XmlController extends Controller
{
    public function show()
    {
        $url = "http://feeds.feedburner.com/blogspot/amDG?format=xml";
        $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>
    <div>
        <a><spam>{{ $item->link }}</spam></a>
        <div>update: {{ $item->updated }} published: {{ $item->published }}</div>
        <br>
        <div>{{ $item->content }}</div>
        <br>
        <div><spam>{{ $item->name }}</spam></div>
        <br>
        <div>{{ $item->uri }}</div>
        <div>{{ $item->email }}</div>  
    </div>
@endforeach

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', 'XmlController@show');
    
answered by 25.08.2017 / 01:21
source