dongyunqin7307 2019-04-01 14:38
浏览 57

如何将.php文件中的scrapper中的url链接到我的html代码

I have a link that came from a Scrapper in a .php file.

I'd like to include that link into a in a .html.twig file

I tried $url and $amazon and $asin but I don't think the structure works.

Basically, this site scraps amazon URLs and identifies the ASIN ID.

There is a ProductController.php file

namespace App\Controller;

use App\Entity\Product; use App\Form\AddProductType; use App\Form\ProductType; use App\Repository\ProductRepository; use App\Service\AmazonScrapper; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use > App\Service\LookingForAsin;

/** * @Route("admin/product") */ class ProductController extends Controller {

/**
 * @Route("/new", name="product_indexAjax", methods="GET|POST")
 * @param Request $request
 *** @param LookingForAsin $lookingForAsin
 * @param AmazonScrapper $amazon**
 * @param ProductRepository $productRepository
 * @return JsonResponse
 */
public function new(Request $request, LookingForAsin $lookingForAsin, AmazonScrapper $amazon, ProductRepository

$productRepository) { $product = new Product(); $error = '';

    $content = json_decode($request->getContent(), true);
    **$asin = $lookingForAsin->extractAsin($content['asin']);
    if (empty($asin)) {**
        return new JsonResponse(
            ['error' => 'Oops, you must enter the url of a product.']
        );
    } else if (!empty($productRepository->findBy(['asin' => $asin]))) {
        return new JsonResponse(
            ['error' => 'Oops, the product already exists.']
        );
    }

    $amazon->init($content['asin']);

    $product->setAsin($asin);

    $product->setTitle($amazon->getTitle());
    $product->setPicture($amazon->getPicture());
    $product->setPrice($amazon->getPrice());
    $product->setRating($amazon->getRating());
    $product->setRatingReview1Star($amazon->getReviewRating(1));
   ````
    $product->setDescription($amazon->getDescription());

    $em = $this->getDoctrine()->getManager();
    $em->persist($product);
    $em->flush();

    if($product->getPrice() == 0) {
        $error = 'Oops, this product is probably unavailable :/';
    }

    return new JsonResponse(
        [
            'template' => $this->renderView('admin/product/item.html.twig',
                [
                    'product' => $product,
                ]
            ),
            'error' => $error,
        ]
    );
}

/**
 * @Route("/", name="product_index", methods="GET|POST")
 * @param Request $request
 * @param ProductRepository $productRepository
 * @return Response
 */
public function index(Request $request, ProductRepository $productRepository)
{
    $product = new Product();
    $form = $this->createForm(AddProductType::class, $product);
    $form->handleRequest($request);
    return $this->render('admin/product/index.html.twig',
        [
            'products' => $productRepository->findBy([], ['id' => 'DESC']),
            'product' => $product,
            'form' => $form->createView()
        ]
    );
}

/**
 * @Route("/{id}", name="product_show", methods="GET|POST")
 * @param Request $request
 * @param int $id
 * @return Response
 */
public function show(Request $request, int $id): Response
{
    $em = $this->getDoctrine()->getManager();
    $product = $em->getRepository(Product::class)->find($id);
    $form = $this->createForm(ProductType::class, $product);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $em->flush();
        return $this->redirectToRoute('product_show', ['id' => $product->getID()]);
    }
    return $this->render('admin/product/show.html.twig',
        [
            'product' => $product,
            'form' => $form->createView(),
        ]
    );
}

/**
 * @Route("/{id}/edit", name="product_edit", methods="GET|POST")
 * @param Request $request
 * @param Product $product
 * @return Response
 */
public function edit(Request $request, Product $product): Response
{
    if ($request->request->all()['name'] == "title") {
        $product->setTitle($request->request->all()['value']);
    } else if ($request->request->all()['name'] == "picture") {
        $product->setPicture($request->request->all()['value']);
    } else if ($request->request->all()['name'] == "description") {
        $product->setDescription($request->request->all()['value']);
    }

    $em = $this->getDoctrine()->getManager();
    $em->persist($product);
    $em->flush();

    return new JsonResponse(
        [
            'picture' => $product->getPicture(),
        ]
    );
} ``

`

Then, this is the .html.twig file

{% extends 'base.html.twig' %}

{% block title %}Wishlist{% endblock %}
{% block stylesheets %}
    <link rel="stylesheet" href="{{ asset('build/index.css') }}">
{% endblock %}
{% block body %}
    <div class="header">
        <img class="logo text-center" src="{{ asset('build/images/logo.png') }}" height="70px">
        <h1>No idea what to get? Check your friend's wishlist here!</h1>
        <div class="form-container">
            {{ form_start(form, {'attr': {'class': 'form-row'}}) }}
            <div class="col mb-3">
                {{ form_widget(form.mail, {'attr': {'class': 'email-input'}}) }}
            </div>
            <button type="submit" class="btn btn-danger"> OK </button>
            {{ form_end(form) }}
        </div>
    </div>


    ** THIS IS WHERE I WANT TO ADD THE URL ***
    <a href="{{ asset('$url') }} > (# link to Amazon page #)
      <div class="container wishlist">
          {% if user.productUsers is defined %}
              {% for products in user.productUsers %}
                  {% set product = products.product %}
                  <div class="row product-card">
                      <div class="product-container-img">
                          <img src="{{ product.picture }}" alt="">
                      </div>
                      <div class="product-container-content">
                          <div class="product-container-header">
                              <div class="product-container-title">
                                  <h2>{{ product.title }}</h2>
                              </div>
                              <div class="product-container-rating">
                                  {{ include('admin/product/rating.html.twig') }}
                              </div>
                          </div>
                          <div><p>{{ product.description|raw }}</p></div>
                          <div class="price">{{ product.price }} €</div>
                      </div>
                  </div>
              {% endfor %}
          {% else %}
              {% set flashes = app.flashes('notice') %}
              {% if flashes|length == 0 %}
                  <div class="arrow-container">
                      <i class="fas fa-arrow-circle-up arrow"></i>
                  </div>
              {% else %}
                  {% for message in flashes %}
                      <div class="flash-notice">
                          {{ message }}
                      </div>
                  {% endfor %}
              {% endif %}
          {% endif %}
      </div>
    </a>
{% endblock %}

I would like to be able to click on that div part and be directed to the product URL

Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?