I am stuck in redirect after user verification from database. Here is the error which i am getting
App\Controllers\Login::index(): Return value must be of type string, CodeIgniter\HTTP\RedirectResponse returned
Here is my code
namespace App\Controllers;
class Login extends BaseController
{
public function index(): string
{
$request = \Config\Services::request();
$session = \Config\Services::session();
$db = \Config\Database::connect();
//$redirectResponse = \config\Services::redirectresponse();
if($request->getPost('email') !=""){
$query = $db->table('users')->getWhere(['email' => $request->getPost('email'), 'password' => md5($request->getPost('password'))]);
if($query->getNumRows() > 0){
$user = $db->table('users')->getWhere(['email' => $request->getPost('email'), 'password' => md5($request->getPost('password'))])->getRow();
$session->set('userid', $user->uid);
return redirect('products');
}else{
$session->setFlashdata('error', 'Account Not Found or Password Incorrect');
}
}
return view('login');
}
}
I tried this too
return redirect()->route('products');
You have incorrectly indicated that the return type of your
index()method is astring.The general principle of the methods contained in a Controller is to work on Request and Response properties. Most of the time, your methods will therefore receive Requests and return Responses or render Views. There is no reason for your index() to return a string.
By the way, you correctly extend the
BaseControllerclass. You therefore inherit its$requestproperty and you don't need to instantiate it yourself with$request = \Config\Services::request();. You can simply use$this->requestinstead.Try this code:
General note: you should in theory move your common logic (session check for example) into the
initControllermethod of theBaseControllerclass. Every Controller will then share it automatically as they inherint from BaseController. Or you can see Filters feature for this specific case. In the same type of question, it's also annoying that you have to connect to your DB in each of your controllers. See if you can centralize that or not depending on your own logic and needs.Finally, be careful, since you are passing passwords (encrypted, admittedly) via the querystring, which can really quick become tricky.