1. Go to this page and download the library: Download cuculcan/core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cuculcan / core example snippets
class User extends Entity {
public $name;
public $email;
public $cityId;
public $login;
public $password;
/**
* @type = json
*/
public $subscription;
/**
* @extra
*/
public $city_name;
/**
* @extra
*/
public $region_name;
public function __construct(array $data = []) {
parent::__construct($data);
}
}
class UsersMysqlStorage extends MysqlStorage {
/**
*
* @param \Pdo $connection
*/
public function __construct($connection) {
parent::__construct($connection, 'users'); //Sets the name of the table with which the storage works
}
/**
* uses the parent class method getById
* @param int $id
* @return User
*/
public function getById($id) {
$data = parent::getById($id);
if (!$data || $data === null) {
return null;
}
return new User($data);
}
/**
* direct query can be implemented using \PDO
* @return int
*/
public function countUsers() {
$sql = " SELECT count(id) AS cnt FROM users ";
$stm = $this->connection->prepare($sql);
$stm->execute();
$cnt = 0;
while ($row = $stm->fetch(\PDO::FETCH_ASSOC)) {
$cnt = $row['cnt'];
}
return $cnt;
}
}
protected function setActions()
get()
post()
del()
put()
$this->get | post | del | put ('[url pattern]', callback function with parameter ($urlParams));
$this->get('/user/{userId}/get_name', function ($urlParams) {
echo $urlParams['userId'];
});
//process GET request http://myexample.com/user/set_name?name=VasiliyPupkin
$this->get('/user/set_name', function ($urlParams) {
$name =$this->request->getQueryParameter("name", "");
echo $name
});
//process GET request http://myexample.com/user
$this->get('/user', function ($urlParams) {
$this->model['some_parameter'] = 'param from user controller';
$this->showView("UserView");
});
class MainView extends AView
{
public function __construct($model) {
parent::__construct();
$this->model = $model;
}
protected function setTemplateName()
{
/*
* the template name is set relative to the path specified in config/config.php
*/
$this->templateName = '/main.php';
}
//--------------------------------- SEO --------------------------------
protected function setTitle(){
$this->title = "";
}
protected function setKeywords() {
$this->keywords="";
}
protected function setDescription(){
$this->description="";
}
//---------------------------------------------------------------------------
protected function setAdditionalCSS(){
array_push($this->additionalCSS, '/css/main.css');
}
protected function setAdditionalJS(){
array_push($this->additionalJS, '/js/main.js');
}
protected function setCustomHeaders(){
array_push($this->customHeaders, "MY-PEW-HEADER: pew-pew-pew");
}
}