2013-07-09 134 views
0

我正在構建一個網站,其中包含一個搜索欄,用於查詢亞馬遜書籍並按照書籍類別和相關性排序。我在我的佈局做了如下形式:將亞馬遜產品搜索API集成到laravel

{{ Form::open(array('action' => 'home/api', 'method' => 'GET', 'class' => 'search1')) }} 
     {{ Form::text('booksearch', 'Search...', array('class' => 'searchinput searchbooks', 'placeholder' => 'Search...')) }} 
      {{ Form::select('category', array(
       '1' => 'All Books', 
       '2' => 'All Textbooks', 
       '3' => 'Business & Finance Textbooks', 
       '4' => 'Communication & Journalism Textbooks', 
       '5' => 'Computer Science Textbooks', 
       '6' => 'Education Textbooks', 
       '7' => 'Engineering Textbooks', 
       '8' => 'Humanities Textbooks', 
       '9' => 'Law Textbooks', 
       '10' => 'Medicine & Health Sciences Textbooks', 
       '11' => 'Reference Textbooks', 
       '12' => 'Science & Mathematics Textbooks', 
       '13' => 'Social Sciences Textbooks' 

      ), '1', array('class'=>'btn btn-primary')) }} 

      {{ Form::select('sort', array(
       '1' => 'Relevance', 
       '2' => 'Alphabetical: A to Z', 
       '3' => 'Alphabetical: Z to A', 
       '4' => 'Bestselling', 
       '5' => 'Average customer review', 
       '6' => 'Price: low to high', 
       '7' => 'Price: high to low', 
       '8' => 'Publication date: newer to older' 

      ), '1', array('class'=>'btn btn-primary')) }} 

      {{ Form::submit('Search', array('class'=>'btn btn-primary submitbook')) }} 

    {{ Form::close() }} 

我有這個在我的routes.php文件:

Route::controller('/', 'HomeController'); 
Route::controller('home/api', '[email protected]'); 

在我的HomeController:

public function getIndex() 
{ 
    return View::make('home.index'); 
} 
public function getSearch() 
{ 
    return View::make('home.api'); 
} 

然後,我設置了一個API文件,我的表單將在視圖/ home/api.blade.php下用以下代碼提交:

<?php 
//Enter your IDs 
define("Access_Key_ID", "myaccesskeyhere"); 
define("Associate_tag", "x00a7-20"); 
//Set up the operation in the request 


if(!empty($_GET['booksearch']) && !empty($_GET['category']) && !empty($_GET['sort'])) { 
    $booksearch = ''; 
    $category = ''; 
    $sort = ''; 
    $SearchIndex = ''; 
    $Keywords = ''; 
} 


function ItemSearch($SearchIndex, $Keywords){ 
//Set the values for some of the parameters 
$Operation = "ItemSearch"; 
$Version = "2011-08-01"; 
$ResponseGroup = "ItemAttributes,Offers"; 
//User interface provides values 
//for $SearchIndex and $Keywords 

//Define the request 
$request= 
"http://webservices.amazon.com/onca/xml" 
. "?Service=AWSECommerceService" 
. "&AssociateTag=" . Associate_tag 
. "&AWSAccessKeyId=" . Access_Key_ID 
. "&Operation=" . $Operation 
. "&Version=" . $Version 
. "&SearchIndex=" . $SearchIndex 
. "&Keywords=" . $Keywords 
. "&Signature=" . [Request Signature] 
. "&ResponseGroup=" . $ResponseGroup; 
//Catch the response in the $response object 
$response = file_get_contents($request); 
$parsed_xml = simplexml_load_string($response); 
printSearchResults($parsed_xml, $SearchIndex); 
} 
?> 

請讓我知道我去錯了,因爲我得到這個錯誤,當我嘗試訪問形式:

Class [email protected] does not exist 

回答

2

Route::controller()沒有得到函數名稱作爲其第二個參數。

您應該將Route::controller('home/api', '[email protected]');行更改爲API控制器,可能命名爲ApiController,它將是Route::controller('home/api', 'ApiController');

然後您可以在app/controllers文件夾中創建一個ApiController.php。創建您的getIndex函數並將搜索邏輯放入其中。

+0

謝謝。我做到了,但現在我得到了錯誤:未知的行動[家庭/ api]。 – TechyTimo

+0

我剛剛編輯了我的答案,你應該定義'getIndex'而不是'getSearch',如果你剛剛創建了Controller,不要忘記編寫'composer dump-autoload'。 –

+0

想象我剛纔那樣做......同樣的錯誤:未知的行動[家庭/ api]。 – TechyTimo