phplaravel-5searchsearch-engine

Search engine in laravel faulty


I am trying to implement Smart Search Engine Here is the Tutorial link
https://github.com/msurguy/laravel-smart-search.
I know this Tutorial for Laravel 4 and I am trying to implement in Laravel 5.2.
Now I'm stuck.
I can't get JSON format of api/search.
Here is my route:

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

Here is controller which I create with the helper of artisan in App/http/controller. I did try to create like tutorial api/searchcontroller but it won't work; always getting error:

"ApiSearchController@index Not found"

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Product;
use Response;

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

When I open http://localhost/search/public/api/search I should be get all data from database like tutorial, but can't get; other thing is same. Please show me what's wrong.


Solution

  • Your search isn't working as it is expecting to be provided with a query to search for as part of a query string or request.

    Input::get('q') is saying to retrieve q from your query string or request and use it for the search.

    When you go to the page http://localhost/search/public/api/search you are doing so as a GET request and so would need to provide your query like so http://localhost/search/public/api/search?q=example.

    The example in the tutorial is using AJAX to run a GET request which does the above for you.

    Try adding ?q=xxxx on to the end of your URL and it should work if you have set it up appropriately following that tutorial.