PHP Beech framework (LTS)
, (*1)
#Make it by yourself
, (*2)
# Environment Requirements
PHP >= 7.1.11
#, (*3)
# Installing Beech
The Beech use composer
to manage its dependencies. So, before using Beech
make sure you have Composer installed on your machine., (*4)
$ composer create-project bombkiml/phpbeech hello-world
#, (*5)
# Local development server
If you have PHP installed locally and you would like to use PHP's built-in development server to your application,
You may use the serve
command. This command will start a development server at http://localhost:8000
, (*6)
$ php beech serve
#, (*7)
# Defining Controllers
Below is an example of a basic controller class. Note that the controller
extends the base controller
class.
The controller are stored in the modules/controllers/ directory. A simple controller modules/controllers/fruits/fruitsController.php
might look something like this:, (*8)
view->yourVariable `` and assign data this one for passing the data to ``` views ```. A simple passing the data might look something like this:
```php
view->title = "fruits page";
$this->view->sayHello = "Hello fruits";
$this->view->data = [];
// Return response view
return $this->view->render("fruits/fruits.view");
}
}
```
#
### # Creating Views
Below is an simple of a basic views contain the HTML served, The ``` views ``` are stored in the ``` views/ ``` directory. A simple view ``` views/fruits/fruits.view.php ``` might look something like this:
```html
Title Name
Hello world
```
#
### # Accessing the data passed from controller
You may use the `$this` for accessing the data passed to views. A simple accessing the data passed might look something like this:
```html
title; ?></title>
</head>
<body>
<!-- Accessing the data passed of @var sayHello -->
<h1><?php echo $this->sayHello; ?></h1>
<!-- Accessing the data passed of @var data -->
<?php print_r($this->data); ?>
</body>
</html>
#, (*9)
# Defining Models
Below is an example of a basic create an model
class. Note that the model
extends the base model
class.
The model
are stored in the modules/models/
directory. A simple model modules/models/Fruits.php
might look something like this:, (*10)
<?php
class Fruits extends Model {
/**
* @Rule: consturctor class it's call __construct of parent class
*
* Call parent class
*
*/
public function __construct() {
parent::__construct();
}
/**
* Simple method using MySQL get data
*
*/
public function getFruits() {
// Preparing sql statements
$stmt = $this->db->prepare("SELECT * FROM fruits");
// Execute statements
$stmt->execute();
// Return response rows
return $stmt->fetch_all();
}
}
#, (*11)
# Database
The Beech database (MySQL supported) using by $this->db
it's query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application.
#, (*12)
# Retrieving Results
You may use the prepare
method on the php $this->db
facade to begin a query. The prepare
method returns a object query builder instance for the given table, allowing you to using sql statements for query by the execute
method then finally get the results using the fetch
method. So, 3 step easy usage you may retrieving results by use the methods like this:, (*13)
-
One: Specify statements, First you must specify your sql statements for get something by using
prepare()
Then prepare function will return new object for call next actions, So following basic for get data something like this:
$stmt = $this->db->prepare("SELECT * FROM fruits");
-
Two: Execute statements, After specify statements you must execute your sql statements by using object
$stmt
as above:
$stmt->execute();
-
Finalize: Response data, Response data using by object
$stmt
for return your result data. So, Have a response are available for using:
$stmt->fetch_all();
// result: array
$stmt->fetch_assoc();
// result: array
$stmt->fetch_array();
// result: array
$stmt->fetch_object();
// result: object
$stmt->num_rows();
// result: int
:grey_question: Tips: You can show your sql statements before execute: $stmt->show();
|
------------ |
#, (*14)
# Database transactions
You may use the transaction method provided by $this->model
facade to run a set of operations within a database transaction. If an exception is thrown within the transaction closure, the transaction will automatically be rolled back. If the closure executes successfully, the transaction will automatically be committed. You don't need to worry about manually rolling back or committing while using the transaction method:, (*15)
// Init autocommit off
$this->db->transaction();
// update, delete some value
$this->db->update("fruits", array("name" => "Cherry"), array("id" => 1));
$this->db->delete("fruits", array("id" => 1));
// commit transaction
if ($this->db->commit()) {
echo "Commit completed!";
} else {
// Rollback transaction
$this->db->rollback();
}
#, (*16)
# Controller calling The Database
-
The model
automatic connect when you make model
under rules "same file name". So if you make Controller name is "Fruits
" you must be make Model name is "Fruits
" same., (*17)
-
You may use the $this->model
for calling all the methods in model. A simple calling the method might look something like this:, (*18)
<?php
class FruitsController extends Controller {
public function __construct() {
parent::__construct();
}
/**
* Simple calling the model `Fruits`
*
*/
public function index() {
// Calling the method in model
$this->view->fruits = $this->model->getFruits(); // <---- Call method in Fruits model
// Return response view
return $this->view->render("fruits/fruits.view");
}
}
#, (*19)
# Inserts
The query builder also provides an insert
method for inserting records into the database table. The insert method accepts an array of column names and values:, (*20)
$this->db->insert("fruits", array("id" => "1", "name" => "Banana"));
#, (*21)
# Updates
The query builder can also update existing records using the update
method. The update
method accepts an array of column and new value pairs containing the columns to be updated. You may constrain the update query using where clauses:, (*22)
$this->db->update("fruits", array("name" => "Cherry"), array("id" => 1));
#, (*23)
# Deletes
The query builder may also be used to delete
records from the table via the delete method. You may constrain the delete
query using where clauses:, (*24)
$this->db->delete("fruits", array("id" => 1));
#, (*25)
# Using with beech-cli
command (recommended)
Document PHP beech command line interface (CLI)
#, (*26)
# Development
Want to contribute or join for Great Job!. You can contact to me via
- GitHub: bombkiml/phpbeech - issues
- E-mail: nattapat.jquery@gmail.com
- Facebook: https://www.facebook.com/bombkiml
#, (*27)
# License
PHP Beech framework is open-sourced software licensed under the MIT license., (*28)