skip to Main Content

Hello I am trying to make a basic CRUD app.
I keep on getting the error "Action appHttpControllersPostController not defined.", but I am not sure why. I am trying to redirect to the PostController after my auth checks the email and password so for redirecting I used
return redirect()->action([PostController::class]);
I think it could be because I am using a resource controller.

My post controller

namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;
use IlluminateSupportFacadesSession;
use AppModelsUser;
use IlluminateSupportFacadesAuth;
use appHttpControllersPostController;


class CustomAuthController extends Controller
{
    public function index()
    {
        return view('login');
    }  
      
    public function customLogin(Request $request)
    {
        $request->validate([
            'email' => 'required',
            'password' => 'required',
        ]);
   
        $credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->intended('dashboard')
                        ->withSuccess('Signed in');
        }
  
        return redirect("login")->withSuccess('Login details are not valid');
    }

    public function registration()
    {
        return view('registration');
    }
      
    public function customRegistration(Request $request)
    {  
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6',
        ]);
           
        $data = $request->all();
        $check = $this->create($data);
         
        return redirect()->action([PostController::class]);
    }

    public function create(array $data)
    {
      return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password'])
      ]);
    }    
    
    public function dashboard()
    {
        if(Auth::check()){
            return redirect()->action([PostController::class]);
        }
  
        return redirect("login")->withSuccess('You are not allowed to access');
    }
    
    public function signOut() {
        Session::flush();
        Auth::logout();
  
        return Redirect('login');
    }
}

2

Answers


  1. You need to update

    return redirect()->action([PostController::class]);
    

    To

    return redirect()->action([PostController::class, 'your_method name']);
    

    OR

    return redirect()->action("PostController@yourmethod");
    
    Login or Signup to reply.
  2. To redirect best and shorthand approach will be to use the route name

    return redirect()->route('posts.index');
    

    Make sure to replace posts.index with your route name.

    and if you route need parameter then it will look like this.

    return redirect()->route('posts.index',['post'=>$post->id]);
    

    here ‘post’ will be parameter name that you will set in route web.php file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search