momik / simple-mvc
Installs: 11
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
pkg:composer/momik/simple-mvc
Requires
- vlucas/phpdotenv: ^5.4
README
A simple PHP MVC framework built from scratch.
Pre-requisites
-
XAMPP or Manual created environment with:
-
PHP 8.0+
sudo apt update sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y sudo add-apt-repository ppa:ondrej/php sudo apt install php8.0 -y sudo apt install php8.0-cli php8.0-common php8.0-mbstring -y -
MySQL DB
sudo apt update sudo apt install mysql-server -
Apache2
sudo apt update sudo apt install apache2
-
-
Composer
sudo apt update cd ~ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.phpHASH=`curl -sS https://composer.github.io/installer.sig` php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composercomposer
Installation
-
Create app using
composer create-project momik/simplemvc example-app -
Navigate to ' public '
cd example-app cd public -
Serve app at port:8080
php -S localhost:8080
Getting Started
-
DATABASE
Edit the.envfile to configure database.
- Set database name in
DB_DSN = ......dbname ='test_db' - Set user in
DB_USER = root - Set password in
DB_PASSWORD = password
- Set database name in
-
Migration
Runmigrations.phpto initialize database
You can manipulate database and tables by adding scripts in 'php migrations.phpmigrations' directory.
- There is no strict naming convention for migration scripts.
- Migration scripts need to be a class with classname = filename.
- Migration class need to have
up()anddown()method. - Run
migrations.phpso that changes take effect.
- There is no strict naming convention for migration scripts.
Documentation
-
Entry point
- The
public/index.phpis the entry point for the application. public/index.phpdetermines how HTTP requests are handled.
Syntax:
Router::METHOD('/url', callback);Example:
Anonymous functions:
Router::GET('/test', function(){ //some code here return "This is test." });Router::POST('/test', function(){ //some code here return "This is test." });Controller functions:
Router::GET('/login', [LoginController::class, 'index']);Router::POST('/login', [LoginController::class, 'login']); - The
-
Controllers
- Write Controllers in
controllersdirectory. YourControllermust extendController.- Controllers must contain property:
public array $params[] - Members/elements of this array property can be accessed as separate variable in respective view.
- Example.
$this->params['message'] = "This is message"; //in controller <p> <?php echo $message;?> // in view </p>$this->params['user']['id'] = 5; //in controller $this->params['user']['name'] = "foo"; $this->params['user']['email'] = "bar"; <p> <?php echo $user['id'];?> // in view <?php echo $user['name'];?> <?php echo $user['email'];?> </p>
- Write Controllers in
-
Models
- Write Data models in
modelsdirectory. YourModelmust extendModel.YourModelmust implement methods:- tableName( ). Returns ( string ) name of table .
- primaryKey( ). Returns ( string ) primary field name .
- fields( ). Returns (array of string ) containing name of all fields except primary field. .
- Write Data models in
-
Views
- Views can be rendered through controller by:
return View::make('home', $this->params); - Write Views in
viewsdirectory. Viewsmust declare document title by://inside view <?php /** @var $this momik\simplemvc\core\View */ $this->title = "Document title"; ?>
- Views can be rendered through controller by:
-
Layouts
- You can have multiple layouts.
- Write layouts in
views/layouts - Set layout through respective controller by:
$this->setLayout('layoutName');
- You can have multiple layouts.
-
Sessions
- Sessions made easy.
- Setting, Getting, Unsetting Session
SESSION::set('key', 'value'); //setting session SESSION::get('key); //accessing set session SESSION::remove('key); //accessing set session - Setting and Getting Session Flash
SESSION::setFlash('key', 'value'); //setting session flash SESSION::getFlash('key); //getting session flash msg
-
Form Validation
- Access predefined validations. ( Study
core/Validation.phpfor all available validations. ) - Example:
$formFields = array( "email"=>"foo@bar.com", "password"=>"fooBar#123" ); $errors = Validation::validate($formFields) //returns array string of error messages. if ( empty($errors) ) { echo "All ok"; } else { foreach ( $errors as $error ) { echo $error."<br>" } }
- Access predefined validations. ( Study
-
Form and Field Components
- Create forms using the Form and Field Component.
- Syntax:
Form::open('actionUrl', 'requestMethod'); echo Form::field('inputType', [assoc array of attribute and values], 'optionalErrorMsg'); echo "<button type='submit'>Login</buton>"; Form::close(); - Example:
Form::open('', "post"); echo Form::field("email", ['name' => 'email', 'placeholder'=>'Email here'], $errors['email'] ?? ''); echo Form::field("password", ['name' => 'password', 'placeholder'=>'Password here'], $errors['password'] ?? ''); echo "<button type='submit' class='btn btn-md btn-primary my-2 col-12'>Login</buton>"; Form::close();
-
Basic CRUD Operation
The
core/Model.phpcontains commonly used CRUD operation.
These operations are inherited by alldataModelsin themodelsdirectory.
Operations:Operation Method Params Description Create record save() - Inserts a record into table. Read Single record by ID fetch($id) $id Fetches record based on primary key. Read Single record by XYZ findOne($where) $where $where is a assoc array. Fetches record where multiple WHEREconditions are satisfied.Update record update($id) $id Updates single record based on id. Delete record delete($id) $id Deletes single record based on id.
FAQs
1. What does $request->getMethod() do?
Returns HTTP Request method. Returns get, post, put, etc.
2. What does $request->getBody() do?
Returns assoc array of HTTP Request content. Mostly used to get POST data from form.
3. What does $object->findOne($assocArray) do?
Returns associative array from respective table where, fields and values match key and value of $assocArray.
4. What does $object->initializeProperty($assocArray) do?
Returns void. Keys of the associative array are initialized as properties of object, values of associative array are set as respective property value.