【问题标题】:Laravel "Controller does not exsist" while route is correct路线正确时,Laravel“控制器不存在”
【发布时间】:2016-02-29 19:30:59
【问题描述】:

我正在尝试使用 ajax 智能搜索,

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

但是我的应用程序找不到上面教程中使用的控制器。 这是我得到的错误:

类 App\Http\Controllers\Api\ApiSearchController 不存在

在谷歌搜索此错误消息后,我发现它是由不正确的路线引起的。但我相信我正确设置了路线。

这是我的路线:

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

这是我的控制器:

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

}

【问题讨论】:

    标签: php ajax laravel controller routes


    【解决方案1】:

    我认为您指定的命名空间不是预期的:

    类 App\Http\Controllers\Api\ApiSearchController 不存在

    不匹配:

    <?php namespace App\Http\Controllers;
    

    【讨论】:

    • 是的,就是这样!现在觉得好傻。但是非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 2019-03-26
    • 2014-11-11
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多