adamjhn
326
13
Laravel

Redirect user to a specific page if he acesses a conference with id that dont exist

Posted 1 year ago by adamjhn

I have this route:

    Route::get('conference/admin/{id}', [ 'uses' => '[email protected]', 'as'=>'conference.admin']);

So when the user acesses "http://proj.test/conference/manage/1" it acesses the page homepage of the conference admin area.

But if this specific conference is removed if the user acessses "http://proj.test/conference/admin/1" it appears a page with this message:

Sorry, the page you are looking for could not be found.

But, if the conference dont exist and the user acesses "http://proj.test/conference/admin/1" what should happen is the user be redirected to the homepage ('/').

Do you know how to achieve that?

I already have in the AuthServiceProvider the code below to allow only the user that is the creator of the conference to acess the admin area of that conference. I dont know if this can be used also to redirect the user to ('/') if he acesses a conference that dont exist.

// auth service provider gate

 public function boot(GateContract $gate)
    {
        $this->registerPolicies();

        $gate->define('access-management-area', function($user, $conference)
        {
            return $user->id == $conference->organizer_id;
        });
    }

Then in the admin() the gate is used:

   public function admin($id){
        $conference = Conference::findOrFail($id);
        if(Gate::allows('access-management-area', $conference)) {
            return view('conference.admin')->with('conference',$conference);
        } else {
            return redirect('/');
        }
    }

Please sign in or create an account to participate in this conversation.