I created a MVC in PHP and i tried to use Eloquent to handle db. But i'm stuck in the begining of it i.e; in Create itself.
my codes :
home.php:-
class Home extends Controller
{
public function create($username='',$email='')
{
User::create([
'username'=>$username,
'email'=>$email
]);
}
}
?>
Controller.php
<?php
class Controller
{
public function model($model)
{
require_once '../app/models/'. $model .'.php' ;
return new $model();
}
public function view($view,$data=[])
{
require_once '../app/views/'. $view.'.php';
}
}
?>
User.php
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
class User extends Eloquent
{
public $name;
protected $fillable=['username','email'];
}
?>
App.php
<?php
class App
{
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
// by default we are considering /public/controller/methodname
//as soon as the directory is written on the address bar the first arg is controller , 2nd is method and the
// others are parameters
public function __construct()
{
$url = $this->parseUrl();
if(file_exists('../app/controllers/'. $url[0].'.php'))
{
$this->controller= $url[0];
unset($url[0]); //remove fromm array
} //this if was to check whether the entered controller exists or not, if exists controller is set to the controllers name
//or home is the default controller
require_once '../app/controllers/'. $this->controller.'.php';
$this->controller= new $this->controller;// create object instance of that controller
if(isset($url[1]))
{
if(method_exists($this->controller, $url[1]))
{
$this->method = $url[1];
unset($url[1]);
}
}
$this->params= $url ? array_values($url) : [];
call_user_func_array([$this->controller,$this->method], $this->params);
}
public function parseUrl()
{
if(isset($_GET['url'])){
return $url = explode('/',filter_var(rtrim($_GET['url'],'/'),FILTER_SANITIZE_URL));
}
}
}
?>
in my users table in Mysql only the created_at and updated_at gets updated after each time i hit the url.
format of url: public/controller/method/param1/param2
url: public/home/create/Johnny/johnny16@live.com