How do I properly extend a Shield Controller? #1022
Answered
by
datamweb
EliMariaaaa
asked this question in
Q&A
|
Hello, I'm trying to extend the <?php
namespace App\Controllers;
use CodeIgniter\Shield\Controllers\RegisterController as ShieldRegister;
use CodeIgniter\HTTP\RedirectResponse;
class RegisterController extends ShieldRegister
{
public function registerView()
{
return echo "test";
}
/**
* Attempts to register the user.
*/
public function registerAction(): RedirectResponse
{
if (auth()->loggedIn()) {
return redirect()->to(config('Auth')->registerRedirect());
}
// Check if registration is allowed
if (! setting('Auth.allowRegistration')) {
return redirect()->back()->withInput()
->with('error', lang('Auth.registerDisabled'));
}
$users = $this->getUserProvider();
// Validate here first, since some things,
// like the password, can only be validated properly here.
$rules = $this->getValidationRules();
if (! $this->validateData($this->request->getPost(), $rules, [], config('Auth')->DBGroup)) {
return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
}
print_r($this->request->getPost()); die();
// Save the user
$allowedPostFields = array_keys($rules);
$user = $this->getUserEntity();
$user->fill($this->request->getPost($allowedPostFields));
// Workaround for email only registration/login
if ($user->username === null) {
$user->username = null;
}
try {
$users->save($user);
} catch (ValidationException $e) {
return redirect()->back()->withInput()->with('errors', $users->errors());
}
// To get the complete user object with ID, we need to get from the database
$user = $users->findById($users->getInsertID());
// Add to default group
$users->addToDefaultGroup($user);
Events::trigger('register', $user);
/** @var Session $authenticator */
$authenticator = auth('session')->getAuthenticator();
$authenticator->startLogin($user);
// If an action has been defined for register, start it up.
$hasAction = $authenticator->startUpAction('register', $user);
if ($hasAction) {
return redirect()->route('auth-action-show');
}
// Set the user inactive
$user->deactivate();//$user->activate();
$authenticator->completeLogin($user);
// Success!
return redirect()->to(config('Auth')->registerRedirect())
->with('message', lang('Auth.registerSuccess'));
}
}I completely replaced what's inside the |
Answered by
datamweb
Feb 13, 2024
Replies: 1 comment 2 replies
|
Hi,
Because you have not set the routes for new controller. To do this you need to unset the default routes( |
2 replies
Answer selected by
EliMariaaaa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Because you have not set the routes for new controller. To do this you need to unset the default routes(
service('auth')->routes($routes, ['except' => ['register']]);) from the circuit and then set up the routes with your new controller.Read the document for more information.