0

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');
    }
}
  • unless your Controller is "invokable" and you defined a route to point directly to the controller and not a method of the controller then you would have to tell the `action` call the method you are referring to on the Controller ... and resource routes setup 7 routes, which are 7 individual routes each pointing to a method on the controller, not the controller itself – lagbox May 16 '23 at 02:06
  • show your route definition ... also `app != App` pretend that case matters – lagbox May 16 '23 at 12:49

2 Answers2

0

You need to update

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

To

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

OR

return redirect()->action("PostController@yourmethod");
Irshad Khan
  • 434
  • 4
  • 13
0

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.

Hassan Fayyaz
  • 685
  • 1
  • 9
  • 15