سؤال

i am building a website with a search-bar that queries amazon books and sorts by book category and by relevance. I made a form on my layout as follows:

  {{ 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() }}

I have this in my routes.php:

Route::controller('/', 'HomeController');
Route::controller('home/api', 'HomeController@Search');

On my HomeController:

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

Then I set up an api file where my form will submit under views/home/api.blade.php with the following code:

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

Please let me know where im going wrong because I get this error when I try to access the form:

Class HomeController@Search does not exist
هل كانت مفيدة؟

المحلول

Route::controller() doesn't get the function name as its 2nd parameter.

You should change the Route::controller('home/api', 'HomeController@Search'); line to an API controller, maybe named as ApiController, and it will be Route::controller('home/api', 'ApiController');.

And then you can create an ApiController.php in your app/controllers folder. Create your getIndex function and put the search logic in it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top