Laravel Route Accept Get And Post Example

10-Apr-2023

.

Admin

Laravel Route Accept Get And Post Example

Hi guys, Today i will explained to the Laravel Route Accept Get And Post method in your laravel project.Laravel Route Accept Get And Post method is so easy to use.so you can just follow my step by step and learn Laravel Route Accept Get And Post Method.

So let's start to the example and follow to the my all step.

Solution


public function register()

{

function get()

{

///

}

function post()

{

///

}

}

Example :

Step 1: Create Route

Last step to create a route in web.php file and use this code.

Route :routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\RegistrationController;

/*

|--------------------------------------------------------------------------

| 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::match(['GET', 'POST'], 'register', 'User\RegistrationController@register')->name('register');

Step 2: Create a RegistrationController Controller

Next you can require to the RegistrationController so create a RegistrationController in just following command through.

Controller : app/Http/Controllers/RegistrationController.php

Style 1

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RegistrationController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function register(Request $request)

{

if ($request->isMethod('GET')) {

return view('user.registration');

}

if ($request->isMethod('POST')) {

$this->validator($request->all())->validate();

event(new Registered($user = $this->create($request->all())));

return $this->registered($request, $user)

?: redirect($this->redirectPath());

}

}

}

Style 2

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RegistrationController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function register()

{

function get()

{

///

}

function post()

{

///

}

}

}

So, finally we are done with our code we can use the code.

#Laravel 8