Я создаю приложение CMS, в котором пользователь должен сначала зарегистрироваться.
Я создал RegisterController
, где я определяю index()
метод, который возвращает представление register.blade.php
!
Когдапользователь нажимает кнопку «Регистрация», запрос должен перейти к методу create()
вместе с /register
URL и создать пользователя.
Так как я уже определил, что /register
должен открыть register.blade.php
, то как мнезапустить другой метод для создания пользователя с тем же URL-адресом?
Я также не хочу использовать 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');