phplaravellaravel-8laravel-controller

Action Redirect with resource controller laravel - Gives error Action app\Http\Controllers\PostController not defined


Hello I am trying to make a basic CRUD app. I keep on getting the error "Action app\Http\Controllers\PostController 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 App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use app\Http\Controllers\PostController;


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');
    }
}

Solution

  • You need to update

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

    To

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

    OR

    return redirect()->action("PostController@yourmethod");