2013-07-11 45 views
0

我正在開發一個網站,您可以從亞馬遜產品廣告api中搜索項目。 我有下面的代碼上的意見/佈局/ master.blade.php一個搜索框:在laravel 4刀片視圖上顯示API xml數據

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

的形式張貼到一個API的文件用下面的代碼:

<?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); 


    ?> 

搜索後你被導航到該文件,並顯示來自亞馬遜的有效xml數據。但正如你所看到的,api文件是我的public/assets/AmazonAPI文件夾中的一個php文件,因此在樣式化xml時無法擴展我的佈局。 請讓我知道我應該怎麼包括在視圖/搜索/ index.blade.php刀片觀點我API代碼,這樣我可以擴展它的佈局,如:

@extends('layouts.mylayout') 

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

也讓我知道正確的方法我應該打開表格。

+0

究竟是你想怎麼辦?你能詳細說明一下嗎? –

+0

我剛剛編輯了這個問題。請讓我知道你需要澄清的地方 – TechyTimo

+0

首先爲什麼要將api代碼放入'public/assets'文件夾?要麼你無法正確解釋你的問題,要麼我無法理解。至於我的理解,您可以在任何控制器函數中調用此文件,並返回具有所需結果的視圖。 –

回答

1

我會引導你在簡單和更多Laravel的方式做到這一點。 所以你可以在app目錄下創建一個文件夾libraries 並將你的amazon api文件放在libraries文件夾中。

現在,在您composer.json添加"app/your_amozon_api_library_folder_name"在類映射,像

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

現在使用composer dump-autoload or php composer.phar dump-autoload 現在你amozon的加載,供全球使用API​​轉儲自動加載。

假設你有一個搜索方法的HomeController的,現在把你的搜索方法API代碼,

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); 
} 
} 
+0

謝謝你。 – TechyTimo