使用 Laravel 的注册表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22048146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:06:01  来源:igfitidea点击:

Registration Form using Laravel

phplaravel

提问by Sainath Krishnan

Im trying to create a basic registration form on Laravel, following Dayle Rees' tutorial, specifically -> This one

我试图按照 Dayle Rees 的教程在 Laravel 上创建一个基本的注册表单,特别是 ->这个

When I load my registration form, everything is fine. But when I submit, it says

当我加载我的注册表时,一切都很好。但是当我提交时,它说

The requested URL /testserver/registration was not found on this server.

I am a beginner wishing to learn Laravel better, so any help is appreciated!

我是一个希望更好地学习 Laravel 的初学者,因此感谢您的帮助!

This is my register.blade.php

这是我的 register.blade.php

<!doctype html>
<html lang="en">

<head>
</head>

<body>
<h1>Registration Form</h1>

{{ Form::open(array('url' => '/registration')) }}

    <ul class="errors">
    @foreach($errors->all() as $message)
        <li>{{ $message }}</li>
    @endforeach
    </ul>


    {{-- Username field. ------------------------}}
    {{ Form::label('username', 'Username') }}
    {{ Form::text('username') }}

    {{-- Email address field. -------------------}}
    {{ Form::label('email', 'Email address') }}
    {{ Form::email('email') }}

    {{-- Password field. ------------------------}}
    {{ Form::label('password', 'Password') }}
    {{ Form::password('password') }}

    {{-- Password confirmation field. -----------}}
    {{ Form::label('password_confirmation', 'Password confirmation') }}
    {{ Form::password('password_confirmation') }}

    {{-- Form submit button. --------------------}}
    {{ Form::submit('Register') }}

{{ Form::close() }}
</body>

</html>

And this is my routes.php

这是我的 routes.php

Route::get('/', function()
{
    return View::make('register');
});

Route::post('/registration', function()
{
    $data=Input::all();

    // Build the validation constraint set.
    $rules = array(
        'username'   => 'required|min:5|max:15',
        'email'      => 'required|email',
        'password'   => 'required|alpha_num|confirm|min:8'
    );

    // Create a new validator instance.
    $validator = Validator::make($data, $rules);

    if ($validator->passes()) {
        // Normally we would do something with the data.
        return 'Data was saved.';
    }

    return Redirect::to('/')->withErrors($validator);

});

Route::filter('csrf', function()
{
    if (Request::forged()) return Response::error('500');
});

Form Action from page source - http://localhost/testserveris my root folder

来自页面源的表单操作 -http://localhost/testserver是我的根文件夹

<form method="POST" action="http://localhost/testserver/registration" accept-charset="UTF-8"><input name="_token" type="hidden" value="LUE1UWguOcyKSbgRyGchfKppRHab504v9p8uEZhQ">

回答by Antonio Carlos Ribeiro

You need to use the method POST in your Route:

您需要在您的路线中使用 POST 方法:

Route::post('/registration', function() ...

Otherwise Laravel will not find that route when you POST your form.

否则,当您发布表单时,Laravel 将找不到该路由。

If your route is still not being found, it's possible that your .htaccess is not rewriting correctly your urls, to testyou can change your form open to:

如果仍未找到您的路线,则可能是您的 .htaccess 没有正确重写您的网址,为了测试您可以将表单更改为:

{{ Form::open(array('url' => '/index.php/registration')) }}

If it works, you'll have to fix your VirtualHost or .htaccess.

如果它有效,您将必须修复您的 VirtualHost 或 .htaccess。