Problem when trying to validate email that already exists in database - php

I am building custom mvc framework in php in order to learn and when I am trying to submit my form with an mail that already exists in the database, my validation should prevent me to do so, instead I get this error:
Fatal error: Uncaught Error: Call to a member function findUserByEmail() on null in C:\xampp\htdocs\gacho\App\Controllers\UsersController.php:
UsersController.php
<?php
namespace App\Controllers;
use App\Models\User;
use Core\Controller;
class UsersController extends Controller
{
public function __construct($controller, $action)
{
parent::__construct($controller, $action);
$this->userModel = $this->load_model('User');
}
public function registerAction()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = [
'email' => trim($_POST['email']),
];
}
if (empty($data['email'])) {
$data['email_err'] = "Please enter your email!!!";
} else {
if ($this->userModel->findUserByEmail($data['email'])) {
$data['email_err'] = "Email is already taken!";
}
}
}
User.php
<?php
namespace App\Models;
use Core\Database;
class User
{
private $db;
public function __construct()
{
$this->db = new Database();
}
public function findUserByEmail($email)
{
$this->db->query('SELECT * FROM users WHERE email = :email');
$this->db->bind(':email', $email);
$row = $this->db->single();
if ($this->db->rowCount() > 0) {
return true;
} else {
return false;
}
}
}
Controller.php:
<?php
namespace Core;
class Controller
{
protected $_controller;
protected $_action;
public $view;
public function __construct($controller, $action)
{
$this->_controller = $controller;
$this->_action = $action;
$this->view = new View();
}
protected function load_model($model)
{
$modelPath = 'App\Models\\' . $model;
if (class_exists($modelPath)) {
$this->{$model.'Model'} = new $modelPath();
}
}
}
I think the mistake is about $this->userModel , but I'm stuck and any help is appreciated.

The problem is that in __construct of UsersController you have:
$this->userModel = $this->load_model('User');
So you assign to userModel property the return value of load_model method.
load_model method doesn't return anything so $this->userModel is always set to NULL, doesn't matter if load_model succeeded or not.
You should just return new $modelPath(); in load_model if you want to assign it to a property by return value.
Also add throw new Exception($modelPath. 'not found'); at the end of load_model method to be sure it did load the model, and not just failed silently to find it.
Note that $this->userModel is not the same as $this->UserModel (case sensitive) and $modelPath = 'App\Models\\' . $model; - why \ after App, and two \ after Models?

I think you need to access your model in $this->UserModel, since User was passed into the load_model method.

Related

How to build login system in Codeigniter 4?

I want to build a login system using Codeigniter 4.
But I face some error.
I have some data in Users_model;
Here is my some codes:
Controller/Signin.php
<?php
namespace App\Controllers;
use App\models\Users_model;
class Signin extends BaseController {
public function index() {
return view('signin/index');
}
public function authenticate() {
if ($this->exists($_POST['email'], $_POST['password']) != NULL) {
$session = session();
$session->set('email', $_POST['email']);
return $this->response->redirect(site_url('signin/profile'));
} else {
$data['msg'] = 'wrong';
return view('signin', $data);
}
}
public function profile() {
return view('signin/profile');
}
private function exists($email, $password) {
$model = new Users_model();
$account = $model->where('email', $email)->first();
if ($account != NULL) {
if (password_verify($password, $account['password'])) {
return $account;
}
}
return NULL;
}
}
Models/Users_model.php
<?php
namespace App\models;
use CodeIgniter\Model;
class Users_model extends Model {
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['first_name', 'last_name', 'email', 'password'];
}
But I face this error:
Please help me.
Or please someone suggest me a login system in another way in Codeigniter 4?
If you want to make login system, I suggest you to use validation to make user is valid and redirect to another controller or view. Then you can use filter to check that user is logged in or not and adding some routes filter to protect other controller.
First read this Codeigniter 4.0.4 documentation https://codeigniter4.github.io/userguide/libraries/validation.html
You could search anything you need there.
return view('signin', $data);
for
return view('signin/index', $data);

UnitTest a PHP CodeIgniter Controller that calls a model

I'm testing my CodeIgniter project with PHPUnit Testing framework (CITest.php). When the function test_model(), calls the model directly to get the details of an user, it works perfectly. But when I do the same via a controller by calling the function test_controller(), it does not output anything (When I debugged, the model itself doesn't gets called). I even verfied if the post data is passed correctly by creating a function test_post_data(). Am I missing something?
I could only find online resources to test the mdoel directly or a controller separately. But I couldn't find any useful link which calls a controller that triggers the model.
CITest.php
class CITest extends PHPUnit_Framework_TestCase
{
private $CI;
public function setUp()
{
$this->CI = &get_instance();
$this->CI->load->model('Test_model');
$this->model = $this->CI->My_model; // load the model
$this->auth = new Test_controller; // load the controller
}
public test_model() {
$user_id = 6;
print_r($this->model->getUserData($user_id));
}
public test_post_data() {
$_POST['useR_id'] = 22;
print_r($this->model->check_post_data());
}
public test_controller() {
$_POST['useR_id'] = 22;
print_r($this->model->get_user_data());
}
}
Test_controller.php
class Test_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Test_model');
}
public function check_post_data() {
return $this->input->post();
}
public function get_user_data() {
$user_id = $this->input->post('user_id');
return $this->Test_model->getUserData($user_id);
}
}
Test_model.php
class Test_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function getUserData($user_id) {
return $this->db->select("*")
->from("users")
->where("user_id", $user_id)
->get()->result_array();
}
}
The code in CITest.php
public test_controller() {
$_POST['useR_id'] = 22;
print_r($this->model->get_user_data());
}
should be like the following?
public test_controller() {
$_POST['useR_id'] = 22;
print_r($this->auth->get_user_data());
}

How do I call model function on create event? Laravel-5

I'm trying to create a referral url when a user is first created.
My function inside my User model looks like this:
private function make_url()
{
$url = str_random(40);
$this->referral_url->url = $url;
if ($this->save()){
return true;
}
else{
return false;
}
}
Within the model, I've tried doing this but didn't work
USER::creating(function ($this){
$this->make_url();
})
I also tried calling it in my User Controller within the create user action
public function create(UserRequest $request)
{
$data = $request->all()
$data['password']= bcrypt($request->input('password'));
if($user=User::create($data))
{
$user->make_url();
}
}
I get this error in return
Indirect modification of overloaded property App\User::$referral_url has no effect
Thanks in advance for your help guys =]
p.s: If there's a better way to go about creating referral urls please tell me.
update
My entire user model
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $table = 'users';
protected $fillable = [
'first_name',
'last_name',
'url',
'email',
'password',
'answer_1',
'answer_2',
'answer_3'
];
protected $hidden = ['password', 'remember_token'];
public function make_url()
{
$url = str_random(40);
$this->referral_url->url = $url;
if ($this->save()){
return true;
}
else{
return false;
}
}
public function user_info()
{
return $this->hasOne('App\UserInfo');
}
public function sec_questions()
{
return $this->hasOne('App\SecurityQuestions');
}
public function referral_url()
{
return $this->hasOne('App\ReferralUrl');
}
}
update
I modified the function in the model to look like this now.
public function make_url()
{
$url = str_random(40);
$referral_url = $this->referral_url;
$referral_url = new ReferralUrl();
$referral_url->user_id = $this->id;
$referral_url->url = $url;
if ($referral_url->save()){
return true;
}
else{
return false;
}
}
When I call
$user->make_url()
I'm able to create it and it shows up in my db, but I also get the error-
Trying to get property of non-object
Normally the creating method should be called within boot():
public static function boot() {
parent::boot();
static::creating(function ($model) {
$model->foo = 'bar';
});
}
This would then be called automatically before the model is saved for the first time.
The problem that I see with your code is that you're attempting to modify a relation which doesn't exist yet.
So to explain, the hasOne method will attempt to join the current model to the remote model (in your case a ReferralUrl model) in SQL, but it can't do that before you save your model because your model doesn't exist in the database.
With your second attempt, the ReferralUrl object is the one that is changing, so that is the one that you need to save:
public function make_url() {
$url = str_random(40);
$referral_url = $this->referral_url
$referral_url->url = $url;
if ($referral_url->save()){
return true;
} else {
return false;
}
}

Call to a member function on null error CodeIgniter

Please dont`t ask why I have more than one controller: this is what my teacher wants.
I have 3 controllers: Api.php, Token.php and Apikey.php, where Api.php is the "main" one.
PROBLEM: When I pass into the URL: http://localhost/revolution/index.php/api/registerUser/first_name/12First
it seems there are problems with loading the Token controller from Api controller.
ERROR: Fatal error: Call to a member function generateToken() on null
What can I do?
API Controller: Api.php
class api extends CI_Controller {
public function index()
{
parent::__construct();
$this->load->library("../controllers/Apikey.php");
$this->load->library("../controllers/Token.php");
}
public function registerUser ($username,$parola)
{
if ($this->isValidUserName($username) && $this->isValidPass($parola)) {
$this->load->library("../controllers/Token");
$id = $this->ApiModel->insertCredentials($username, md5($parola));
$this->Token->generateToken($id);
$this->ApiKey->generateApiKey($id);
$data['registered'] = 1;
$this->load->view('api', $data);
}
}
}
Controller Token.php
public function existsToken($token)
{
$arrayTokens = $this->ApiModel->getAllTokens();
if (in_array($token, $arrayTokens))
return existsToken(sha1($this->randomString())); //$this->isValidToken(sha1($this->randomString()));
return $token;
}
public function randomString() {
return intval(993432422 % rand());
}
public function generateToken($id_user)
{
$token = $this->existsToken(sha1($this->randomString()));
$date = $this->generateExpDate();
$result = $this->ApiModel->insertToken($token, $date, $id_user);
}

Laravel 4: Pass validation messages obtained from repository to controller

Learning about Ioc and Repositories and stuck at last hurdle!
Assuming I am validating input, how do I pass back messages from the Validator within the repository to the controller?
UserRepository
interface UserRepository {
public function all();
public function create($input);
public function findById($id);
}
Sentry2UserRepository
class Sentry2UserRepository implements UserRepository {
...
public function create($input) {
$validation = Validator::make($input, User::$rules);
if ($validation->passes()) {
Sentry::createUser( array_except( $input, ['password_confirmation']));
// Put something here to tell controller that user has been successfully been created
return true;
}
else {
// pass back to controller that validation has failed
// with messages
return $validation->messages(); ?????
}
...
My UserController
UserController extends BaseController {
...
public function postRegister() {
$input['first_name'] = Input::get('first_name');
$input['last_name'] = Input::get('last_name');
$input['email'] = Input::get('email');
$input['password'] = Input::get('password');
$input['password_confirmation'] = Input::get('password_confirmation');
// Something like
if ($this->user->create($input)) {
Session::flash('success', 'Successfully registered');
return Redirect::to('/');
}
else {
Session::flash('error', 'There were errors in your submission');
return Redirect::to('user/login')->withErrors()->withInput();
}
}
...
}
Only 1.5 weeks into Laravel so please go easy on me.
Assuming your repository is working fine for you already:
class Sentry2UserRepository implements UserRepository {
public $validation;
public function create($input) {
$this->validation = Validator::make($input, User::$rules);
if ($this->validation->passes()) {
Sentry::createUser( array_except( $input, ['password_confirmation']));
// Put something here to tell controller that user has been successfully been created
return true;
}
else {
// pass back to controller that validation has failed
// with messages
return false;
}
}
}
Then you just have to access it within your controller using
$this->user->validation->messages()

Categories