我正在try 使用REST风格的控制器.这是我的Route.php美元:

Route::resource('test', 'TestController');
Route::get('/', function()
{
    return View::make('hello');
});

这是我的TestController.php美元

<?php
class TestController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
            return View::make('test.home');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
            //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
            //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
            //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
            //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
            //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
            //
    }
}

我的应用程序路径是localhost/Test/public/,显示"你已经到达"消息.但当我try localhost/Test/public/test时,它给出了"Symfony\Component\HttpKernel\Exception\NotFoundHttpException"

这是我的日志(log):

[2014-05-11 14:29:59] production.ERROR: NotFoundHttpException Route: `http://localhost/Test/public/test` [] []
[2014-05-11 14:29:59] production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in C:\wamp\www\test\bootstrap\compiled.php:5289
Stack trace:
#0 C:\wamp\www\test\bootstrap\compiled.php(4663): Illuminate\Routing\RouteCollection->match(Object(Illuminate\Http\Request))
#1 C:\wamp\www\test\bootstrap\compiled.php(4651): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#2 C:\wamp\www\test\bootstrap\compiled.php(4643): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#3 C:\wamp\www\test\bootstrap\compiled.php(698): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#4 C:\wamp\www\test\bootstrap\compiled.php(679): Illuminate\Foundation\Application->dispatch(Object(Illuminate\Http\Request))
#5 C:\wamp\www\test\bootstrap\compiled.php(1136): Illuminate\Foundation\Application->handle(Object(Illuminate\Http\Request), 1, true)
#6 C:\wamp\www\test\bootstrap\compiled.php(7218): Illuminate\Http\FrameGuard->handle(Object(Illuminate\Http\Request), 1, true)
#7 C:\wamp\www\test\bootstrap\compiled.php(7815): Illuminate\Session\Middleware->handle(Object(Illuminate\Http\Request), 1, true)
#8 C:\wamp\www\test\bootstrap\compiled.php(7762): Illuminate\Cookie\Queue->handle(Object(Illuminate\Http\Request), 1, true)
#9 C:\wamp\www\test\bootstrap\compiled.php(10768): Illuminate\Cookie\Guard->handle(Object(Illuminate\Http\Request), 1, true)
#10 C:\wamp\www\test\bootstrap\compiled.php(640): Stack\StackedHttpKernel->handle(Object(Illuminate\Http\Request))
#11 C:\wamp\www\test\public\index.php(49): Illuminate\Foundation\Application->run()
#12 {main} [] []

I know this question has been asked many times. I had gone through many relevant threads but just can not figure out the solution.

推荐答案

A NotFoundHttpException exception in Laravel always means that it was not able to find a router for a particular URL. And in your case this is probably a problem in your web server configuration, virtual host (site) configuratio, or .htaccess configuration.

你的公众/.htaccess应该是这样的:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

正如你所看到的,在第一行IfModule mod_rewrite.c中有一个条件,所以如果你没有安装或启用"重写模式",所有的重写规则都会失败,这是错误的

localhost/Test/public/

都会工作得很好,但不是这样:

localhost/Test/public/test

另一方面,这个也应该可以工作,因为这是它的原始形式:

localhost/Test/public/index.php/test

因为拉威尔需要重写它才能工作.

And note that you should not be using /public, your URLs should look like this:

localhost/Test/

这是虚拟主机的文档根未正确配置或未指向/var/www/Test/public或应用程序所在的任何路径的另一个线索.

All this assuming you are using Apache 2.

Laravel相关问答推荐

Laravel 通过 API 嵌套 url 发布

Laravel 数据未传递到下拉框

如何避免谷歌翻译翻译:参数

Laravel 5 - 更改模型文件位置

Laravel Eloquent 查找日期超过 2 天的行

集成测试模拟外观与注入模拟

为什么 laravel 模型会重复一组数据以及如何(如果可能)只有一组数据?

Laravel,没有shell 访问的转储自动加载

如何监控 Laravel 队列是否正在运行?

Laravel Auth::attempt() 返回 false

Laravel Session 总是改变 Laravel 5.4 中的每个刷新/请求

Laravel 4,如何测试复选框是否被选中?

PHP 中的 Http 响应代码枚举

在 Laravel Blade 中转义 vue.js 数据绑定语法?

Laravel - 自定义时间戳列名称

Heroku CLI 安装错误:PATH 未更新,原始长度 1585 > 1024

Laravel 中的单会话登录

Laravel 5.3 通知 - 仅使用Electron邮件地址通知

在 laravel 的自定义路径中创建模型

Laravel如何仅响应没有正文消息的204代码状态