Skip to content

All topics  /  Laravel

Laravel Route Accept Get And Post Example

Laravel ·

Teams applying “Laravel Route Accept Get And Post Example” in a production project can use this team workflow page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

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.