Hamad Essa
Hamad Essa

Reputation: 81

Laravel Stop Registration from admin panel

I building settings page where the admin can change some setting in the website I want to add an option to stop registration

for example:

if registration is disabled and a user trying to go to register page he will automatically redirect to 404

Setting Table:

Name: The Name of option or setting

Value: The value (if this value = 0 that means this option is disabled and if it's 1 that means it's enabled)

I already add a column in setting table "stop_register"

what I want is when the value of this column is 0 then registration is off and when it's 1 then the registration is on

Upvotes: 0

Views: 208

Answers (3)

akash_poojary
akash_poojary

Reputation: 53

You can use middleware to do this task,

To create a new middleware, use the make:middleware Artisan command:

php artisan make:middleware CheckRegistration

The above command will create CheckRegistration class in app/Http/Middleware directory.

In this middleware, you can apply your logic to allow registration route or not depending on the value

Middleware:

<?php

namespace App\Http\Middleware;

use Closure;

use App/Registration;

class CheckRegistration
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $value = Registration::select("value"); // assuming value is either 0 or 1

        if ($value == 0) {
            return redirect('404'); // view with 404 display error
        }

        return $next($request);
    }
}

As in the above code, it will redirect to 404 error view if value is 0 otherwise, the request will be passed further into the application.

Code is not tested.

Reference here

Upvotes: 3

Julius Fasema
Julius Fasema

Reputation: 902

try this version:

public function Register()
{
  $stop_reg = DB::table('settingstable')->value('stop_register');
  if( $stop_reg==1 )
   {
     return view('register_page');
   }
  elseif( $stop_reg==0 )
   {
     return view('404_page');
   }

}

Upvotes: 1

Julius Fasema
Julius Fasema

Reputation: 902

try something like this on your blade. though i have not tested the code.

@php $stop_reg = DB::table('settingstable')->value('stop_register'); @endphp

@if($stop_reg==1)

    <a class="nav-link" href="{{ route('register') }}" style="font-size:11px">Register</a>

@elseif($stop_reg==0)

    <a class="nav-link" href="{{ route('stop_register') }}" style="font-size:11px">Register</a>

@endif

on the above code snippet, declare two routes; one for registration page and the second for 404 page. On the controller, have two functions, for registration page and 404 page.

Upvotes: 0

Related Questions