Laravel 路由不适用于 MAMP

Laravel routing not working on MAMP

我刚开始学习 laravel,我正在使用 MAMP。我在这个 url: http://localhost:8888/laravel-site/my-new-app/public/ducks but when I submit the form it takes me to this url: http://localhost:8888/ducks 有一个表格,但没有留在我期望的地方。

routes.php 文件包含以下内容:

Route::get('ducks', function() 
{
    return View::make('duck-form');
});

// route to process the ducks form
Route::post('ducks', function()
{

    // process the form here

});

在我的 .htaccess 文件中我有这个:

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

    RewriteEngine On

    RewriteBase /laravel-site/my-new-app/public/

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

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

我可能遗漏了一些非常明显的东西,但为什么当我提交表单时它没有停留在同一页面上?

检查您的应用 url 是否在 config/app 中正确设置。php:

'url' => 'http://localhost:8888/laravel-site/my-new-app/public',

并确保您的表单提交到正确的路径。

Form::open(array('route' => 'ducks'))

只需使用命名路由即可。应该有帮助。

Route::get('ducks', array('as' => 'showForm', function() 
{
  return View::make('duck-form');
}));

// route to process the ducks form
Route::post('ducks', function()
{

// the get route is named as showForm
  return Redirect::route('showForm');
});

您还没有提供表单的 HTML,但我的猜测是您硬编码了如下内容,并期望 Laravel 使其正常工作:

<form method="post" action="/ducks">

但是,Laravel 不会干扰您的 HTML。相反,您需要使用表单助手来为您创建表单。试试这个:

{{ Form::open(['url' => 'ducks']) }}

这应该为您提供正确的 URL。记住不要在 URL 开头使用 /,Laravel 已经假定所有 URL 都将相对于应用程序根目录(尽管不一定是 Web 服务器文档根目录).

除此之外,您还应该采纳此处提到的其他建议:

  • app/config/local/app.php
  • 中将应用程序的 URL 设置为 http://localhost:8888/laravel-site/my-new-app/public
  • 使用命名路由在您的 HTML
  • 中保存硬编码 URL