0

我正在建立一个带有搜索栏的网站,该搜索栏可以查询亚马逊书籍并按书籍类别和相关性进行排序。我在布局上制作了一个表格,如下所示:

  {{ 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', 'HomeController@Search');

在我的 HomeController 上:

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

然后我设置了一个 api 文件,我的表单将在 views/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 HomeController@Search does not exist
4

1 回答 1

2

Route::controller()没有将函数名称作为其第二个参数。

您应该将该Route::controller('home/api', 'HomeController@Search');行更改为 API 控制器,可能命名为ApiController,它将是Route::controller('home/api', 'ApiController');.

然后你可以ApiController.php在你的app/controllers文件夹中创建一个。创建您的getIndex函数并将搜索逻辑放入其中。

于 2013-07-09T06:13:14.007 回答