-
Notifications
You must be signed in to change notification settings - Fork 5
Routing And Database
The Route
class is responsible for defining routes and their associated handlers in the application. It provides methods for specifying various types of HTTP routes such as GET, POST, etc. and mapping them to corresponding controller actions or closures.
To define a route, you can use one of the following methods on the Route
class:
-
Route::get($path, $controller, $prefix = null)
- Defines a route for the GET HTTP method. -
Route::post($path, $controller, $prefix = null)
- Defines a route for the POST HTTP method. -
Route::put($path, $controller, $prefix = null)
- Defines a route for the PUT HTTP method. -
Route::delete($path, $controller, $prefix = null)
- Defines a route for the DELETE HTTP method. -
Route::any($path, $controller, $prefix = null)
- Defines a route for any HTTP method. -
Route::options($path, $controller, $prefix = null)
- Defines a route for the OPTIONS HTTP method. -
Route::patch($path, $controller, $prefix = null)
- Defines a route for the PATCH HTTP method.
The $path
parameter specifies the path for the route. The $controller
parameter specifies the controller method to execute when the route is matched. The $prefix
parameter is an optional prefix to prepend to the path.
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
You can also create your routes as closures like this:
Route::get('/api', function () {
echo "hello";
});
You can group related routes together and apply a common prefix to all of them using the group
method. The group
method takes two parameters: a prefix and a callback function that defines the routes within the group.
Route::group('/api', function () {
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
});
API-Monster supports the use of controllers, enabling you to organize your API logic into separate classes for better code structure and maintainability.
To create a controller, you can create a new PHP file in the App/Controllers
directory of your API-Monster project. The file should contain a class that extends the Controller
class.
namespace Monster\App\Controllers;
class UserController
{
public function index()
{
// Handle GET request to /users
}
public function store()
{
// Handle POST request to /users
}
public function update($id)
{
// Handle PUT request to /users/{id}
}
public function delete($id)
{
// Handle DELETE request to /users/{id}
}
}
To map a controller method to a route, you can specify the controller and method in the second parameter of the Route
methods.
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@delete');
To pass parameters to a controller method, you can include them as parts of the route path using curly braces.
Route::get('/users/{id}', 'UserController@show');
In the controller method, you can retrieve the parameter value using the corresponding variable name.
namespace Monster\App\Controllers;
class UserController
{
public function show($id)
{
// Handle GET request to /users/{id}
echo "user id: " . $id;
}
}
API-Monster provides a powerful and intuitive routing system that enables you to build high-performance APIs with ease. By following the conventions outlined in this document, you can create well-structured and maintainable API applications.
The DB
class in the Monster\App\Models
namespace provides an easy-to-use interface for basic MySQL database operations. It is designed to work with the PDO extension in PHP and supports insert, select, update, delete, count, sum, average, minimum, and maximum operations on a MySQL database.
To use this class in your application, you can create a new instance of the DB
class and pass in the required parameters for the database connection. For example:
$db = new Monster\App\Models\DB('localhost', 'mydatabase', 'myusername', 'mypassword');
Once you have an instance of the DB
class, you can use its methods to perform operations on the database. For example, to insert a new row into a table, you can call the insert
method and pass in the name of the table and an associative array of column names and values:
$data = array(
'name' => 'John Doe',
'email' => '[email protected]',
'phone' => '123-456-7890'
);
$db->insert('users', $data);
Similarly, to retrieve rows from a table that match certain criteria, you can call the select
method and pass in the name of the table, the columns to retrieve (default is '*'), an associative array of conditions for the query, and an associative array of options for the query:
$conditions = array(
'name' => 'John Doe',
'email' => array('LIKE' => 'example.com')
);
$options = array(
'ORDER BY' => 'name ASC',
'LIMIT' => 10
);
$results = $db->select('users', '*', $conditions, $options);
To update rows in a table that match certain criteria, you can call the update
method and pass in the name of the table, an associative array of column names and new values, and an optional WHERE clause for the query:
$data = array(
'phone' => '555-555-5555'
);
$where = 'name = ?';
$bindings = array('John Doe');
$db->update('users', $data, $where, $bindings);
To delete rows from a table that match certain criteria, you can call the delete
method and pass in the name of the table and an optional WHERE clause for the query:
$where = 'name = ?';
$bindings = array('John Doe');
$db->delete('users', $where, $bindings);
To count the number of rows in a table that match certain criteria, you can call the count
method and pass in the name of the table and an optional WHERE clause for the query:
$where = 'name = ?';
$bindings = array('John Doe');
$count = $db->count('users', $where, $bindings);
To calculate the sum of a column in a table that matches certain criteria, you can call the sum
method and pass in the name of the table, the name of the column to sum, and an optional WHERE clause for the query:
$where = 'name = ?';
$bindings = array('John Doe');
$sum = $db->sum('users', 'balance', $where, $bindings);
To calculate the average of a column in a table that matches certain criteria, you can call the avg
method and pass in the name of the table, the name of the column to average, and an optional WHERE clause for the query:
$where = 'name = ?';
$bindings = array('John Doe');
$avg = $db->avg('users', 'age', $where, $bindings);
To find the minimum value of a column in a table that matches certain criteria, you can call the min
method and pass in the name of the table, the name of the column to find the minimum of, and an optional WHERE clause for the query:
$where = 'name = ?';
$bindings = array('John Doe');
$min = $db->min('users', 'age', $where, $bindings);
To find the maximum value of a column in a table that matches certain criteria, you can call the max
method and pass in the name of the table, the name of the column to find the maximum of, and an optional WHERE clause for the query:
$where = 'name = ?';
$bindings = array('John Doe');
$max = $db->max('users', 'age', $where, $bindings);
Note that all of the methods that accept a WHERE clause also accept an array of bindings for any placeholders in the WHERE clause. This helps to prevent SQL injection attacks.
To see examples, please check out the EasySQL repository at https://github.com/ReactMVC/EasySQL. However, instead of using the EasySQL class, you should use the DB
class in the API-Monster framework.
- 1 - Installation
- 2 - Routing And Database
- 3 - HTTP Request
- 4 - CORS
- 5 - Env File
- 6 - Views
- 7 - Language
- 8 - SPA Without API
- 9 - Data Validation
- 1 - Installation
- 2 - Routing And Database
- 3 - HTTP Request
- 4 - CORS
- 5 - Env File
- 6 - Views
- 7 - Language
- 8 - SPA Without API
- 9 - Data Validation