dsframework / dsframework
The Dsframework template for dsframework project
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Language:Hack
Type:project
Requires
- daevsoft/framework: ^3.0
- firebase/php-jwt: ^6.10
- maximebf/debugbar: 1.*
Requires (Dev)
- phpunit/phpunit: 10
README
PHP DsFramework Next Generation MVC framework. Inspired by Laravel and CodeIgniter framework with cutting edge complexity proccess.
Getting Started
DsFramework support for PHP 8.1 or latest to work properly.
Requirements :
Installation
To create a new project with dsframework type a composer create-project dsframework/dsframework mywebfoldername
command in the terminal.
How to run?
First, install dependencies from composer with composer install
command in terminal.
Second, run a project with php ds serve
command. Done.
Connect Database
To connecting into database, open the .env
file and set your database configuration in the name prefix DB_
property.
Example:
DB_DRIVER=mysql DB_HOST=localhost DB_USERNAME=root DB_PASSWORD=mypassword DB_NAME=somethingdb DB_PORT=3306 SSL_CERT= SSL_VERIFY=false
Leave DB_NAME
blank if a web application does not require a database.
ENV File
The .env
file is a constant value for the application configuration. When the file has been modified, refresh the configuration cache with the php ds config
command in the terminal.
Model
Open terminal and write a command : php ds add:model modelName
Or generate multiple model : php ds add:model modelname1 modelname2 modelnameOther
Example : php ds add:model People
then model file app/models/People.php
will be generated
namespace App\Models; use Ds\Foundations\Connection\Models\DsModel; class People extends DsModel { public $table = 'people'; }
Controller
Open terminal and write a command : php ds add:controller ControllerName
Or generate multiple controller : php ds add:controller controllername1 controllername2 controllernameOther
Example of controller :
class IndexController extends Controller { public function index() { view('home'); } public function peopleList(){ $data = People::all(); // Response is json encode return [ 'people_list' => $data ]; } public function savePeople(Request $request){ // Save json data People::save($request->json()); // OR specify request field People::save([ 'fullname' => $request->fullname, 'phone' => $request->phone ]); // OR all Request Form Field Data People::save($request->all()); } public function welcomePage() { // Response is Views/Html render view('welcome'); } }
Routing
The routing of web application is defined in the app/route/web.php
file.
Example :
// with callback controller method Route::get('/', [IndexController::class, 'index']); Route::get('/welcome', [IndexController::class, 'welcomePage']); Route::get('/people', [IndexController::class, 'peopleList']); Route::post('/people/save', [IndexController::class, 'savePeople']); // simple route Route::get('/sample/subsample', function () { echo 'Welcome to routing!'; }); // With uri as parameter Route::get('/sample/{arg1}/subsample/{arg2}', function ($arg1, $arg2) { echo 'Uri param ' . $arg1 . ' - ' . $arg2; }); // With middleware Route::middleware(['auth'], function () { Route::get('/mypage/{arg1}/othersub/{mysub}', function ($arg1, $mysub) { echo 'page param ' . $arg1 . ' - ' . $mysub; }); Route::get('/mypage/page/{arg1}/{arg2}', function ($arg1, $arg2) { echo 'page ' . $arg1 . ' param ' . $arg2; }); }); // or with middleware in spacific route Route::get('/people-list', [ IndexController::class, 'index' ])->middleware('api-auth'); // or multiple middleware Route::get('/people-list', [ IndexController::class, 'index' ]) ->middleware([ 'api-auth', 'company-auth' ]); // With grouping /admin/... Route::group('admin', function () { Route::get('/get-string', function () { // will return Json Encode return ['username' => 'Deva Arofi']; }); });
View
Open terminal and write a command : php ds add:view viewname
Or generate multiple view : php ds add:view viewname1 viewname2 viewnameOther
Or generate in subdirectory : php ds add:view pages/viewname
and views/pages/
directory will generate automatically.
Example view file : welcome.pie.php
<html> <head> <title>{{ $appname }}</title> </head> <body> Welcome to Web App </body> </html>
Pie Cheat Sheet
Syntax | Closing | Description |
---|---|---|
{{ ... }} |
- |
Same as echo(...) in php |
<< ... >> |
- |
Same as <?php ... ?> in php |
@slot(..) |
- |
Create a slot for templating |
@use(..) |
- |
To use a template that includes @slot syntax |
@part(..) |
@endpart |
To inject a content into @slot(..) |
@foreach(..): |
@endforeach |
Same as <?php foreach(..): in php |
@if(...): |
@endif |
Same as <?php if(..): ?> in php |
Advanced
Middleware
class AuthMiddleware implements Middleware { public function handle(Request $request, $next): Response|null { // if middleware was passed if(true){ return $next($request); }else{ return null; } } }
Assign middleware into Route
Route::get('/all-person', [ PersonController::class, 'index' ]) ->middleware(['auth']);
Passing data from Middleware to Controller
class AuthMiddleware implements Middleware { public function handle(Request $request, $next): Response|null { // find person data by id $personData = Person::find($request->person_id); // if middleware was passed if($personData != null){ $request->add('person', $personData); return $next($request); }else{ return null; } } ... }
Then retrieve data in controller
class PersonController extends Controller { public function index(Request $request) { $person = $request->person; var_dump($person); } ... }
Routing
If you want to grouping routes based on controller, you can write like this :
app/route/web.php
Route::group('/user', UserController::class);
Then, in controller class :
app/controllers/UserController.php
class UserController extends Controller { #[Get('/all')] public void index() { ... } #[Get('/detail/{userId}')] public void userById($userId) { ... } #[Post('/save')] public void saveUser(Request $request) { ... } #[Delete('/delete')] public void deleteUser() { ... } }
Then, you can access by address like this :
GET | http://localhost:8000/user/all
GET | http://localhost:8000/user/detail/3
POST | http://localhost:8000/user/save
DELETE | http://localhost:8000/user/delete
Testing
Dsframework support for testing and unit test.
Write php ds add:test SampleTest OtherTest
in terminal, and test file will generate automatically into \tests
folder.
class SampleTest extends TestCase{ public function test_sample(){ $expect = 'hello'; $this->assertTrue('hello' == $expect); } }
Unit Test
Or, using --unit
options to generate unit test file.
php ds add:test --unit SampleTest
command.
describe('Count is one thousand', function(){ $count = 0; for ($i=0; $i < 1000; $i++) { $count += $i; } return Assert::check($count == 1000); }); // OR mock DatabaseProvider to support Model describe('One is one number', function(){ mock(DatabaseProvider::class); $account = Account::find(7); return Assert::equal($account->id, 7); });
Run Test
To run your test file, type a command php ds test
or php ds test --unit
for unit test file. Then, all test files will be executed.
based on @daevsoft/dsframework