我正在创建一个CMS应用程序,用户应首先注册。
我创建了一个RegisterController
,我在其中定义index()
方法,该方法返回视图register.blade.php
!
当用户点击“注册”按钮时,请求应与create()
网址一起传递到/register
方法并创建用户。
因为我已经定义/register
应该打开register.blade.php
,那么如何运行另一种方法在同一个网址下创建用户呢?
我也不想使用php artisan make:controller RegisterController --resource
。
RegisterController.php
<?php
namespace App\Http\Controllers\Authentication;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RegisterController extends Controller
{
public function index() {
return view('auth.register');
}
protected function validator(array $data) {
return $data->validate([
'fname' => 'required|string|max:255',
'lame' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
]);
}
protected function create(Request $request) {
$this->validator($request->all)->validate();
}
}
register.blade.php
@extends('master')
@section('title')
Register - CMS APP
@endsection
@section('styles')
<link rel="stylesheet" href="{{ asset('css/auth.css') }}">
@endsection
@section('register')
<form class="form-signin" method="post" accept-charset="utf8" action="/register/create">
@csrf
<img class="mb-4" src="http://logo.kenh.net/logo/bootstrap-4.svg.png" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Please Register</h1>
<label for="inputFirstName" class="sr-only">First Name</label>
<input name="fname" type="string" id="inputFirstName" class="form-control" placeholder="First Name" autofocus value="{{old('fname')}}">
@if($errors->has('fname'))
<div class="alert-danger">
<p>First Name is required</p>
</div>
@endif
<label for="inputLastName" class="sr-only">Last Name</label>
<input name="lname" type="string" id="inputLastName" class="form-control" placeholder="Last Name" autofocus value="{{old('lname')}}">
@if($errors->has('lname'))
<div class="alert-danger">
<p>Last Name is required</p>
</div>
@endif
<label for="inputEmail" class="sr-only">Email address</label>
<input name="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" autofocus value="{{old('email')}}">
@if($errors->has('email'))
<div class="alert-danger">
<p>Email is required</p>
</div>
@endif
<label for="inputPassword" class="sr-only">Password</label>
<input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password">
@if($errors->has('password'))
<div class="alert-danger">
<p>Password is required</p>
</div>
@endif
<button class="btn btn-lg btn-success btn-block" type="submit">Register</button>
<br>
<a href="/login" class="btn btn-primary">Login</a>
</form>
@endsection
路线(web.php):
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('register', 'Authentication\RegisterController@index');
Route::get('/login', function() {
return view('auth.login');
})->name('login');
答案 0 :(得分:0)
为什么不使用Laravel Auth为您构建auth数据库,视图和逻辑?
php artisan make:auth
https://laravel.com/docs/5.6/authentication
编辑:
好的,所以如果我理解正确,你想要你的帖子并指向相同的路线网址吗?
Route::match(array('GET','POST'),'/register', 'RegisterController@index');
{
//
});
答案 1 :(得分:0)
您可以尝试以这种形式进行路由:
Route::get('/user', 'UserController@index');
这可能适合你。
另外,请不要忘记浏览Laravel关于路由的官方文档。
https://laravel.com/docs/5.6/routing
这将有助于你。
答案 2 :(得分:0)
表单正在向某个路径发送一个帖子请求,在这种情况下是/register
,但您还没有提供该路由。
Route::post('register', 'Authentication\RegisterController@create');
此外,您的表单操作可以是action="register"