Skip to content

ogoregen/sturdy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sturdy

Sturdy is a web application microframework adopting Model-View-Controller design pattern inspired by Django. It features database abstraction, URL routing and dispatching, cookie based messages, and templating.

Any contribution or feedback is welcome.

All requests are directed to index.php (specified in .htaccess) where the page is constructed by matching the requested url to the app's urls specified in urls.php and by calling the corresponding view function.

Models

Model classes represent database tables, and their instances database records. After they are created in models.php, the corresponding database table needs to be created manually.

A model representing an user is used for demonstration in this document.

Model structure:

class User extends Model{

    public $id;        //
    public $email;     //
    public $password;  //
    public $firstName; //
    public $lastName;  // database fields as class properties as they appear in the database

    function fullName(){ //utility methods can be created

        return $this->firstName." ".$this->lastName;
    }
}

Creating a record:

$newUser = new User();
$newUser->email = "[email protected]";
$newUser->firstName = "new";
$newUser->lastName = "user";
$newUser->save(); //it's now in the database!

Fetching and updating a record:

$user = User::get("email = [email protected]");
//do whatever you'd like, such as changing a field:
$user->email = "[email protected]";
$user->save();

Filtering records:

$users = User::filter("firstName = 'John'"); //all users with the name john

Fetching all records:

$users = User::all(); //all users

Checking if a record exists:

if(User::exists("email = [email protected]"){
  //do things
}

Deleting records

Delete specific object

$user->delete();

Delete one or more

User::deleteWhere("id > 3");

Views

Creating Views

A view represents a page that is rendered or an action like logging out. It processes data and passes them to templates or redirects to another view. Templates contain html and may render php variables passed to them.

Views are functions in views.php. They also need to be added to the urls array in urls.php:

$urls = [
   //...
   "pathToView" => "nameOfView"
];

This record defines a view that is served on /pathToview by the function nameOfView.

A view rendering a template:

function ourNewView(){

   $context = [
      //this array is for data that will be used in the template
      "fruit" => "apple",
      "color" => "green
   ];
   render("ourNewTemplate.php", $context)
   //or if you're not passing variables:
   render("ourNewTemplate.php");
}

A view redirecting:

function ourNewView(){

   //do some business stuff
   header("Location: /"); //redirect home
}

Messages

Cookie based messages that can be retrieved from different views after redirecting.

require "sturdy/messages.php";

Creating a Message

addMessage("success", "This is a success message.");

Retrieving Messages

$messages = getMessages();

Upon retrieving, messages are deleted.

Displaying Messages

<?php foreach($messages as $message): ?>
  <div class="<?= $message["level"] ?>">
    <p><?= $message["body"] ?></p>
  </div>
<?php endforeach ?>

About

PHP web app framework

Resources

License

Stars

Watchers

Forks

Languages