2013-07-09 135 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