PHP code example of dawidgorecki / litemvc

1. Go to this page and download the library: Download dawidgorecki/litemvc 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/ */

    

dawidgorecki / litemvc example snippets


System::setEnvironment(System::ENV_DEVELOPMENT);
System::setEnvironment(System::ENV_PRODUCTION);

$router->add("", "Page", "view");
$router->add("pages/checkout", "Page", "checkout");

$router->add("user/edit/{id}", "User", "editUser");

public function before()
{
    // e.g. checking authentication

    if (!Session::userIsLoggedIn()) {
        $errorController = new ErrorController();
        $errorController->error403();
        exit();
    }
}

public function after() 
{
    // do something
}

$model = $this->getModel();
$view = $this->getView();

$userModel = $this->loadModel("User");

public function error500()
{
    $this->getView()->render('Templates/Errors/error500', ['serverAdmin' => getenv('SERVER_ADMIN')]);
}

$this->getView()->getPDF('file_name_without_extension', 'viewName');

\Libraries\Core\View::checkForActive('controller@action');

$db = $this->getDB();

// Prepare a statement for execution 
$db->prepare("SELECT title FROM pages WHERE id = :id LIMIT 1");

// Bind a value to a parameter
$db->bind("id", $id);

// Execute a prepared statement and return result set
return $db->executeAndFetchAll();

// INSERT INTO users(name,email) VALUES('John','[email protected]')  
$user = new User();
$user->setName("John");
$user->setEmail("[email protected]");
$user->save();

// SELECT * FROM users WHERE id=1 LIMIT 1
$user = User::findById(1);
echo $user->getName();

// SELECT * FROM users
$users = User::findAll();
foreach ($users as $user) {
    echo $user->getName();
}

// SELECT * FROM users WHERE email='[email protected]'
$users = User::findByQuery("SELECT * FROM users WHERE email=:email", [":email" => "[email protected]"]);
echo $users[0]->getName();

// UPDATE users SET name='Edwin' WHERE id=1
$user = User::findById(1);
$user->setName("Edwin");
$user->save();

// DELETE FROM users WHERE id=1
$user = User::findById(1);
$user->delete();

$rowCount = User::getRowsCount();

if (!CaptchaUtils::checkCaptcha($userInput)) {
    // wrong captcha code
} else {
    // code ok
}

$mailer = new Libraries\Core\Mail();
$mailer->addRecipient('[email protected]', 'John Doe');
$mailer->addContent('Subject', 'My message');

if ($mailer->sendMessage('[email protected]', 'Peter Doe')) {
    // message was send
} else {
    // error
    die($mailer->getError());
}

System::log("No route matched", __FILE__, System::E_NOTICE);

$error = new \Controllers\ErrorController();
$error->error404();