سؤال

I am developing a website where you search items from amazon product advertising api. I have a search box on views/layouts/master.blade.php with the following code:

    {{ Form::open(array('url' => 'AmazonAPI/api.php', 'method' => 'GET')) }}
        {{ Form::text('itemsearch', 'Search ...', ) }}
    {{ Form::submit('Search') }}

The form is posting to an api file with the following code:

    <?php
        if(isset($_GET['booksearch'])) {
            /* Example usage of the Amazon Product Advertising API */
            include("amazon_api_class.php");

            $obj = new AmazonProductAPI();
            $result ='' ;
            try
            {
                $result = $obj->searchProducts($_GET['booksearch'],
                                               AmazonProductAPI::DVD,
                                               "TITLE");
            }
            catch(Exception $e)
            {
                echo $e->getMessage();
            }

            print_r($result->Items);


    ?>

After searching you are navigated to the file and it displays a valid xml data from amazon. But as you can see, The api file is a php file in my public/assets/AmazonAPI folder hence I cant extend my layouts when styling the xml. Please let me know how I should include my API code in a views/searches/index.blade.php blade view such that I can extend a layout on it like:

@extends('layouts.mylayout')

@section('content')
//the api code goes here
@stop

Also let me know the correct way I should open the form.

هل كانت مفيدة؟

المحلول

I will guide you to do this in a simple and more Laravel Way. so you can create a folder libraries under app directory and place your amazon api files in the libraries folder.

Now in your composer.json add "app/your_amozon_api_library_folder_name" in classmap , something like

"autoload": { 
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/your_amozon_api_library_folder_name",

Now dump your autoload using composer dump-autoload or php composer.phar dump-autoload Now you amozon api's are loaded for global use.

Suppose you have a HomeController with search method, now put you api codes in search method,

public function search(){
  if(isset($_GET['booksearch'])) {
        /* Example usage of the Amazon Product Advertising API */
        //include("amazon_api_class.php"); no need to include

        $obj = new AmazonProductAPI();
        $result ='' ;
        try
        {
            $result = $obj->searchProducts($_GET['booksearch'],
                                           AmazonProductAPI::DVD,
                                           "TITLE");
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }

        //print_r($result->Items);
        return View::make('your view name')->with('items',$result->Items);
 }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top