Доброго времени суток уважаемые программисты. Извиняюсь за гомнокод я 5 день как перешёл с процедурного стиля.
есть некий код хотел бы переписать в view()->with() но как этого добиться? Не могу понять для чего нужны трейты и инерфейсы
Я ещё только начал читать Design Patterns in PHP and Laravel, PHP_Obekty_shablony_i_metodiki_programmirovania но пока не нашёл такого шаблона правда прочёл немного ещё
$router->get('/signup', function()
{
\Trap\ViewHandle\View::view(['signup']);
});
и вот который будет в конструкторах вызываться
protected function view(array $template = [], array $v = [])
{
View::view($template, $v);
}
<?php
error_reporting(E_ALL);
require_once __DIR__ . '/../app/bootstrap.php';
<?php
spl_autoload_register(function(string $class_name)
{
$class_name = str_replace('\\', '/', $class_name);
$folders = __DIR__ . "/{$class_name}.php";
if (file_exists($folders)) require_once $folders;
});
$app = new Trap\Application();
$app->router = new Trap\Routing\Router($app);
$app->router->group([], function($router)
{
require_once __DIR__ . '/routes/web.php';
});
/*
echo '<pre>';
print_r($app);
echo '</pre>';
*/
<?php
$router->get('/', function()
{
\Trap\ViewHandle\View::view(['signin']);
});
$router->get('/signup', function()
{
\Trap\ViewHandle\View::view(['signup']);
});
$router->post('/submit/signin', 'UserController@signIn');
$router->post('/submit/signup', 'UserController@signUp');
$router->get('/id/(\d+)', 'UserController@getUser'); //(\w+)
$router->run();
<?php
namespace controllers;
use models\UserModel;
class UserController extends Controller
{
private $firstname;
private $lastname;
private $email;
private $password;
private $hash;
private $id;
public function signIn()
{
if ($this->isEmptyPost('signin'))
{
if ($this->isEmptyPost('email') && $this->isEmptyPost('password'))
{
$this->email = $this->clearStr($_POST['email']);
$this->password = $this->clearStr($_POST['password']);
if ($selectUser = UserModel::selectUser($this->email))
{
$this->hash = $selectUser['password'];
if (password_verify($this->password, $this->hash))
{
$this->id = $this->clearInt($selectUser['user_id']);
$_SESSION['user_id'] = $this->id;
$this->redirectTo("/id/{$this->id}");
}
else $this->redirectTo('/');
}
else $this->redirectTo('/');
}
else $this->redirectTo('/');
}
}
public function signUp()
{
if ($this->isEmptyPost('signup'))
{
if
(
$this->isEmptyPost('firstname') &&
$this->isEmptyPost('lastname') &&
$this->isEmptyPost('email') &&
$this->isEmptyPost('password')
)
{
$this->firstname = $this->clearStr($_POST['firstname']);
$this->lastname = $this->clearStr($_POST['lastname']);
$this->email = $this->clearStr($_POST['email']);
$this->password = $this->clearStr($_POST['password']);
if (!$selectUser = UserModel::selectUser($this->email))
{
$this->password = password_hash($this->password, PASSWORD_BCRYPT);
if ($this->id = UserModel::inserttUser($this->firstname, $this->lastname, $this->email, $this->password))
{
$this->id = $this->clearInt($this->id);
$_SESSION['user_id'] = $this->id;
$this->redirectTo("/id/{$this->id}");
}
else $this->redirectTo('/signup');
}
else $this->redirectTo('/signup');
}
else $this->redirectTo('/signup');
}
}
}
<?php
namespace controllers;
use Trap\Http\Request;
use Trap\Sanitize\Validate;
use Trap\ViewHandle\View;
abstract class Controller
{
use Request;
use Validate;
protected function view(array $template = [], array $v = [])
{
View::view($template, $v);
}
}
<?php
namespace models;
class UserModel extends Model
{
static public function selectUser(string $email)
{
parent::instance();
return parent::selectOldCrud('SELECT `user_id`, `email`, `password` FROM `user` WHERE `email` = ?', [$email]);
}
static public function inserttUser(string $firstname, string $lastname, string $email, string $password)
{
parent::instance();
return parent::insertCrud('INSERT INTO `user`(`firstname`, `lastname`, `email`, `password`) VALUES (?, ?, ?, ?)', [$firstname, $lastname, $email, $password]);
}
}
<?php
namespace models;
use PDO;
abstract class Model
{
static private $dbh;
static private $stmt;
static protected $instance;
final protected function __construct()
{
try
{
require_once __DIR__ . '/../config/config.php';
self::$dbh = new PDO($dsn, $user, $pass, $attribute);
}
catch (PDOException $e)
{
echo "Error!: " . $e->getMessage();
exit;
}
}
static protected function instance()
{
if (!static::$instance)
{
static::$instance = new static();
}
return static::$instance;
}
static private function snipetCrud(string $sql, array $params = [])
{
self::$stmt = self::$dbh->prepare($sql);
if (isset($params) && is_array($params))
{
$total = count($params);
for ($p = 0; $p < $total; $p++)
{
if (is_string($params[$p]))
{
self::$stmt->bindParam($p + 1, $params[$p], PDO::PARAM_STR);
}
else if(is_int($params[$p]))
{
self::$stmt->bindParam($p + 1, $params[$p], PDO::PARAM_INT);
}
}
}
self::$stmt->execute();
}
static protected function selectOldCrud(string $sql, array $params = [])
{
self::snipetCrud($sql, $params);
if(self::$stmt->rowCount() > 0) return self::$stmt->fetch(PDO::FETCH_ASSOC);
else return false;
}
static protected function selectAllCrud(string $sql, array $params = [])
{
self::snipetCrud($sql, $params);
if(self::$stmt->rowCount() > 0) return self::$stmt->fetchAll(PDO::FETCH_ASSOC);
else return false;
}
static protected function insertCrud(string $sql, array $params = [])
{
self::snipetCrud($sql, $params);
if(self::$stmt->rowCount() > 0) return self::$dbh->lastInsertId();
else return false;
}
static protected function updateCrud(string $sql, array $params = [])
{
}
static protected function deleteCrud(string $sql, array $params = [])
{
}
}
<?php
namespace Trap\ViewHandle;
class View
{
static public function view(array $template = [], array $v = [])
{
extract($v);
require_once __DIR__ . '/../../views/index.php';
}
}