0
votes

I am trying to use ajax smart search,

http://maxoffsky.com/code-blog/laravel-shop-tutorial-3-implementing-smart-search/

But my application can not find the controller I used from the tutorial above. This is the error i get:

Class App\Http\Controllers\Api\ApiSearchController does not exist

After googling this error message I found out that it is caused by an incorrect route. But I believe I set my routes correctly.

This is my route:

Route::get('api/search', 'Api\ApiSearchController@index');

And here is my controller:

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Base\Controller;

class ApiSearchController extends Controller {

public function appendValue($data, $type, $element)
{
    // operate on the item passed by reference, adding the element and type
    foreach ($data as $key => & $item) {
        $item[$element] = $type;
    }
    return $data;
}

public function appendURL($data, $prefix)
{
    // operate on the item passed by reference, adding the url based on slug
    foreach ($data as $key => & $item) {
        $item['url'] = url($prefix.'/'.$item['slug']);
    }
    return $data;
}

public function index()
{
    $query = e(Input::get('q',''));

    if(!$query && $query == '') return Response::json(array(), 400);

    $products = Product::where('published', true)
        ->where('name','like','%'.$query.'%')
        ->orderBy('name','asc')
        ->take(5)
        ->get(array('slug','name','icon'))->toArray();

    $categories = Category::where('name','like','%'.$query.'%')
        ->has('products')
        ->take(5)
        ->get(array('slug', 'name'))
        ->toArray();

    // Data normalization
    $categories = $this->appendValue($categories, url('img/icons/category-icon.png'),'icon');

    $products   = $this->appendURL($products, 'products');
    $categories  = $this->appendURL($categories, 'categories');

    // Add type of data to each item of each set of results
    $products = $this->appendValue($products, 'product', 'class');
    $categories = $this->appendValue($categories, 'category', 'class');

    // Merge all data into one array
    $data = array_merge($products, $categories);


    return Response::json(array(
        'data'=>$data
    ));
}

}

1

1 Answers

2
votes

I think the namespace you specified isn't the one expected :

Class App\Http\Controllers\Api\ApiSearchController does not exist

doesn't match :

<?php namespace App\Http\Controllers;