Custom Endpoints is where we can create our own custom method or routes.
Let’s try this code below:
I will try to fetch all records from my Orders table using the custom code, currently there are 2 records in it..
public function __ACTION_NAME__(Request $request){
$sqltext = "SELECT * FROM Orders";
$records = DB::select($sqltext);
return $records;
}
public function __ACTION_NAME__(Request $request){
$sqltext = "SELECT * FROM Orders";
$records = DB::select($sqltext);
return $records;
}
router.get('__ACTION_PATH__', async (req, res) => {
try{
let sqltext = `SELECT * FROM Orders` ;
let records = await sequelize.query(sqltext, {type: sequelize.QueryTypes.SELECT });
return res.ok(records);
}
catch(err) {
return res.serverError(err);
}
});
@__tablename_blueprint.route('__ACTION_PATH__', methods=['GET'])
#AuthRequiredDecorator
def __ACTION_NAME__():
try:
sqltext = "SELECT * FROM Orders"
records = db.session.execute(sqltext).all()
return jsonify([dict(row) for row in records])
except Exception as ex:
return abort(500, str(ex))
Note: PyRad Vue
Pay attention with your code indention and it is case-sensitive.
[HttpGet]
__allowanonymous
public ActionResult __ACTION_NAME__(){
try{
var sqlText = "SELECT * FROM tablename";
var records = DB.RawSqlQuery(sqlText);
return Ok(records);
}
catch (Exception ex){
return StatusCode(500, $"{ex}");
}
}
There are multiple ways to test the code:
For PHPRad Classic we need to manually define the a routes in routes/web.php
Route::get('testing','OrdersController@testing')->name('orders.testing');
For PHPRad Vue we need to manually define the a routes in routes/api.php
Route::get('orders/testing','OrdersController@testing');
NOTE: DEFAULT PORT
8060 - Backend API
8050 - FrontEnd
For NodeRad Vue you can directly navigate to this link below:
http://localhost:8060/api/orders/testing
NOTE: DEFAULT PORT
8060 - Backend API
8050 - FrontEnd
For PyRad Vue you can directly navigate to this link below:
http://localhost:8060/api/orders/testing
NOTE: DEFAULT PORT
8060 - Backend API
8050 - FrontEnd
The output should be like this below:
[
{
"id": 1,
"date": "2021-08-02",
"order_no": "12345",
"remarks": "test1"
},
{
"id": 2,
"date": "2021-08-02",
"order_no": "12346",
"remarks": "test2"
}
]