PHP code example of php-mohamed-nabil / marrow-framework

1. Go to this page and download the library: Download php-mohamed-nabil/marrow-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

php-mohamed-nabil / marrow-framework example snippets

 
composer create-project php-mohamed-nabil/marrow --prefer-dist myapp



define('CoreStart',microtime());

use Core\Request;
use Core\Response;
use Core\http\Kernal;

htOn();

$kernal->handle(new Request,$app);

$kernal->lightOff();




namespace Core\Http;

use Core\Request;
use Optimus\Onion\Onion;
use Core\App;
use Core\Lightes\LightesFaced;
use Core\Lightes\Lightes;
use Dotenv\Dotenv;
use Core\Session\Storage\SessionStorage;
use Core\Session\SessionFactory;
use Spatie\Ignition\Ignition;
use Core\Configs\Config;

class Kernal{


    protected $app_middlewares=[];

    public $lightes;

    public function __construct()
    {   
       $dot_env = Dotenv::createMutable(ROOT_PATH);
       $dot_env->load();
         
       app()::$config      = Config::getInstance();

       app()->session      = SessionFactory::create(SessionStorage::class,



namespace Core\Lightes;
use Core\Lightes\LightesInterface;


class Lightes implements LightesInterface{
     
    private  $service_rooms;

	public function __construct(array $rooms)
	{
        $this->setRoom($rooms);
	}

	public function setRoom($rooms)
	{

           $this->service_rooms=$rooms;
	}

	public function on()
	{ 
		
		foreach($this->service_rooms as $service)
		{
			   $service =  app()::$container->get($service);
              
			   app()::$container->resloveClassMethod($service,'register');
			   app()::$container->resloveClassMethod($service,'startup');
			  
		}

	}

	


	public function off()
	{
        
	}


}



namespace Core\Lightes;


interface LightesInterface{

       public function on();

       public function off();
}

public function handle(Request $request,App $app)
    {  
        $onion =  new Onion;
        
        $onion->layer($this->middlewares())->peel($request, function($request){
                return $request;
            });

        return $app->run();
    }


    public function lightOn()
    {  
       $this->enviroment();
       $this->lightes->turnOn();
       $this->setTimeZone();
    }

    public function lightOff()
    {   
        //after output services here
         return $this->lightes->turnOff();
    }


use App\Core\Route\Router as Route;
use App\Controllers\ProductController;
use App\Controllers\UserController;

Route::get('/',function(){
   return view('home');
});

Route::middlewares('api',function(){
    
   Route::prefix('api/',function(){
      
      Route::resource(['product'=>ProductController::class]);

      Route::post('/user/register',[UserController::class,'store']);
      Route::get('/users/',[UserController::class,'allUsers'])->middleware('checktoken');
      Route::post('/user/login',[UserController::class,'userAuth']);

      Route::get('/user/profile',[UserController::class,'profile'])->middleware('checktoken');
      
   });



});


use App\Core\Route\Router as Route;
use App\Controllers\ProductController;
use App\Controllers\UserController;

Route::get('/user/:id',[UserController::class,'profile']);


use App\Core\Route\Router as Route;
use App\Controllers\ProductController;
use App\Controllers\UserController;

Route::get('/user/:id',[UserController::class,'profile'])->regx('(\d+)$');

//webiste address and routes
define('ROUTES_WEB',APP.'routes');
define('SITE_URL','http://localhost:8000/');
define('SITE_AD_URL','http://localhost:8000/admin/');
define('VENDOR',ROOT_PATH.'vendor'.DS);


use App\Middlewares\csrf;
use App\Middlewares\PostSize;
use App\Middlewares\test;
use App\Middlewares\XcsrfCookie;
use App\Middlewares\ViewValidationError;
use App\Middlewares\ApiMiddlware;
use App\Middlewares\isLogedIn;

return[
        //startup application middleware here
      'web'=>[
               ViewValidationError::class,
               csrf::class,
               XcsrfCookie::class,
           
        ],
        //routes middlewares here example: ['middleware_name'=>'middleware']
      'route'=>[
           'test'        => test::class,
           'api'         => ApiMiddlware::class,
           'checktoken'  => isLogedIn::class
      ]
];



namespace App\Startups;

use App\Repositories\BookInterface;
use App\Repositories\BookRepository;
use App\Core\Container\Container;
use App\Startups\StartupInterface;
use App\Core\Database\NativeDB;
use App\Core\Request;
class TimeZoneStartup implements StartupInterface
{
   //timezone service provider if you are saving timezone in db
   //and wants to change it before application startup
      
      public function startup()
      {  
        config()->set('date_default_timezone_set','Europe/Moscow');

        //example:'Europe/Moscow'     
      }

      public function register()
      {   
         
      }

      
      
}





use App\Startups\ProductStartup;
use App\Startups\TimeZoneStartup;

return[

     ProductStartup::class,
     TimeZoneStartup::class

];




namespace App\Controllers;

use Core\Controller;
use Style\Style;
use Core\Request;

class HomeController extends Controller
{  
	  

	 public function __construct()
	 {
	 	//middleware_array,except_methods array(optional)
        $this->middleware(['test',['home']]);
	 }





namespace App\Models;

use Core\Model;

class UserModel extends Model
{  
	//you have to set table attributes her $id,$column_2,$column_3 ...
	//you should set table name attribute or we can predict it like User to be users ,UserCategory user_categories
    protected $mass = ['username','email']; // for mass assignment
	
}


             // $model = new user(12);
		//$model->columns['model_title']='updated';   
		//$model->columns['model_price']=2500000;  
         
       
        //$model->create(['model_title'=>'coding model']); //for mass assignment
		//$model->save(); //for insert
		//$model->amend();// for update
		//$model->purge(); //for delete
		//$model->deleteSoft(12); //for soft delete  


 $user = new user;
$user->get() // all users
$user->get(12) //user with id = 12
$user->create(['username'=>'hambola','password'=>123]); // create new user hambola with new password
$user->purge(12); remove user number  12 with (id=12)
$user->update($this->model->table,['username'=>'hmobla edited'],['id'=>12]); edit user hambola with id =12



namespace App\Repositories;

use App\Repositories\ProductRepositoryInterface;
use App\Models\Product;
use App\Core\Database\NativeDB;

 $users = NativeDB::getInstance()->table('users')->paginate(10); returns array first element is data object ,seconde is key 'links' which has pagination links
 $links = $users?$users['links']:[];

             foreach($users[0] as $user)
              {
                 echo $user->username.':'.$user->id;
              }

NativeDB::getInstance()->table('users')->select('username')->limit(10)->run();  //username from users table limi 10 records
NativeDB::getInstance()->table('users')->select('username')->where('id','=',12)->run(); //username from users table where id =12
NativeDB::getInstance()->table('users')->select('username')->where('id','=',12)->where('username','=','hambola')->run();   //username from users table where id =12 and username = hambola
NativeDB::getInstance()->table('users')->select('username')->group_by('id')->run(); //username from users table groub by id
NativeDB::getInstance()->table('users')->insertInto(['username'=>'hambola brother'])->run(); //insert new user from users table
NativeDB::getInstance()->table('users')->deleteRow(['username'=>'hambola brother'])->run(); // delete where username hambola borhter
NativeDB::getInstance()->table('users')->updateRow(['username'=>'hambola sister'],['id'=>13],$whereoperator='',$soft_delete=false)->run(); // update  where id = 13 (keep two last params as example till further updates).



namespace App\Migrations;

use Core\Database\NativeDB;

class users__2023_09_03_17_56_59{
      
     //$db for database object
	public function up($db)
	{
          $db->query('CREATE TABLE IF NOT EXISTS  `users` (
                 `id` BIGINT NOT NULL AUTO_INCREMENT,
                 `username` varchar(256) NOT NULL,
                 `password` VARCHAR(255) NOT NULL ,
                 `created` datetime NOT NULL,
				 `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
				PRIMARY KEY (`id`)

             )');
           

	}

     public function down($db)
     { 

       $db->query('DROP TABLE users');

     }
 php migrate 
 php create_migration (migration_name) 
 php migrate role=(all)
 php migrate role=(migration_name)
 php create_controller (controllername) 
 php create_controller (controlername) resource 
 php create_controller (controlername) resource model 
 php create_controller (controlername) model 
 php create_model (modelname) 
 php create_repo (repositoryname)