Smyphp
Smyphp is a lightweight PHP framework built for developers who need a simple framework to create web applications
Install / Use
/learn @SegunCodes/SmyphpREADME
SMYPHP
- Description
- Requirements
- Installation
- Usage
- Contributing and Vulnerabilities
- License
DESCRIPTION
Smyphp is a lightweight PHP framework built for developers who need a simple framework to create web applications
REQUIREMENTS
- php 7.3^
- composer
INSTALLATION
$ composer create-project seguncodes/smyphp yourProjectName
USAGE
STARTING APPLICATION
CD into your projects directory and run your application using the command below
$ php smyphp --start
Now you open http://localhost:8000 in your browser to see your application.
OR open with your preferred port
$ php smyphp --start --port 3344
Now you open http://localhost:3344 in your browser to see your application.
Run the following command for help
$ php smyphp --help
DATABASE MIGRATION
All migration files should be saved in the migrations folder. The user_migrations.php is a default migration file and can be used as a boiler plate for creating other migration files.
To migrate the migration files, cd into your projects directory and use this command to perform a database migration
$ php migrate.php
ROUTES
The routes folder contains the assets folder where css, javascript, image and other files can be stored. The routes folder also contains the index.php file which is used to handle all routing.
Rendering Pages
Rendering can be done directly in the index.php file , an example is this
$app->router->get('/hello', function(){
return "Hello world";
});
Visit http://localhost:8000/hello. You're done.
OR rendering could be done using the MVC method , an example is this
index.php file
use App\Http\Controllers\ExampleController;
$app->router->get('/hello', [ExampleController::class, 'examplePage']);
then in the ExampleController.php file
namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
class ExampleController extends Controller{
public function examplePage(){
return $this->render('yourFileName');
}
}
finally in the views folder, in the yourFileName.php file
<h2>Hello World</h2>
Visit http://localhost:8000/hello. You're done.
Rendering Pages With Parameters
Pages can be rendered with parameters using the MVC method...
index.php file
use App\Http\Controllers\ExampleController;
$app->router->get('/hello', [ExampleController::class, 'examplePage']);
then in the ExampleController.php file
namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
class ExampleController extends Controller{
public function examplePage(){
return $this->render('yourFileName', [
'text' => 'hello world'
]);
}
}
finally in the views folder, in the yourFileName.php file
<h2> <?php echo $text ?> </h2>
Visit http://localhost:8000/hello. You're done.
Views and Layouts
The Views folder contains the layouts folder, and also contains files that will be displayed on the browser. The layouts folder contains layouts files. NOTE: main.php file is the default file.
Here is an example of defining a layout for a view file:
example.php file
<div>
<h2>Hello World</h2>
</div>
In layouts folder
main.php file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
</head>
<body>
{{content}}
<script src="assets/js/jquery-3.3.1.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</body>
</html>
The {{content}} is used to display the content of example.php with the layouts from main.php file.
Defining Custom Layout for views
If you do not wish to use the main.php file to render files, then do the following:
- create a new file in the layouts folder
- define this new layout file in the controller function that is handling its rendering
ExampleController.php file
namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
class ExampleController extends Controller{
public function examplePage(){
$this->setLayout('yourLayoutName');
return $this->render('yourFileName');
}
}
The $this->setLayout() function is used to set the layout for a particular page, and should be called before the rendering of the page you are setting a layout for.
Passing params into routes
Params can be passed into routes and queried in controllers, here is an example:
index.php file
use App\Http\Controllers\ExampleController;
$app->router->get('/hello/{id}', [ExampleController::class, 'examplePage']);
then in the ExampleController.php file
namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Http\Request;
class ExampleController extends Controller{
public function examplePage(Request $request){
echo '<pre>';
var_dump($request->getParams());
echo '</pre>';
return $this->render('yourFileName');
}
}
$request->getParams() is used to get the parameters passed in the url
FORMS
Forms can be used in the framework using the default HTML forms or using the Framework's form builder method
Form Builder
Using the Form builder method, in any of your view files , for example a login form...
in login.php in views directory
<?php $form = \SmyPhp\Core\Form\Form::start('', 'post')?>
<?php echo $form->input($model, 'email') ?>
<?php echo $form->input($model, 'password')->Password() ?>
<br>
<div class="input-group">
<input type="submit" class="btn btn-block btn-primary" value="Submit">
</div>
<?php \SmyPhp\Core\Form\Form::stop()?>
The Form::start() method is used to start the form and takes two arguments (action, method).
The $form->input() method is used to call an input field in the form, it takes in two arguments (model, inputName). The model parameter is used to reference the Model handling the request for that form; while the inputName is the name for that input field.
Handling Form Data
Form data is handled using controllers. Here is an example:
in register.php in views directory
<?php $form = \SmyPhp\Core\Form\Form::start('/register', 'post')?>
<?php echo $form->input($model, 'email') ?>
<?php echo $form->input($model, 'password')->Password() ?>
<br>
<div class="input-group">
<input type="submit" class="btn btn-block btn-primary" value="Submit">
</div>
<?php \SmyPhp\Core\Form\Form::stop()?>
Then in index.php the route is defined
use App\Http\Controllers\ExampleController;
$app->router->post('/register', [ExampleController::class, 'register']);
finally in the ExampleController.php file
namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Http\Request;
use App\Models\User;
class ExampleController extends Controller{
public function register(Request $request){
$this->setLayout('auth');
$user = new User();
//$user references the User model
if($request->isPost()){
//your registration logic comes here
return $this->render('register', [
'model' =>$user //this is the model being sent to the form in the register page
]);
}
return $this->render('register', [
'model' =>$user //this is the model being sent to the form in the register page
]);
}
}
Input Types
The form builder also comes with various input types
<?php $form = \SmyPhp\Core\Form\Form::start('', 'post')?>
<?php echo $form->input($model, 'password')->Password() ?>
<?php echo $form->input($model, 'number')->TypeNumber() ?>
<?php echo $form->input($model, 'checkBox')->CheckBox() ?>
<?php echo $form->input($model, 'date')->TypeDate() ?>
<?php echo $form->input($model, 'file')->TypeFile() ?>
<?php echo $form->input($model, 'radio')->TypeRadio() ?>
<br>
<div class="input-group">
<input type="submit" class="btn btn-block btn-primary" value="Submit">
</div>
<?php \SmyPhp\Core\Form\Form::stop()?>
//for text area field
echo new TextareaField($model, 'textarea')
Custom Form Labels
The default labels of input fields in the form builder method are the inputNames of the field. The labels can be changed in the model referenced in the `input(
