PHP code example of seguncodes / smyphp

1. Go to this page and download the library: Download seguncodes/smyphp 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/ */

    

seguncodes / smyphp example snippets


$app->router->get('/hello', function(){
    return "Hello world";
});

use App\Http\Controllers\ExampleController;

$app->router->get('/hello', [ExampleController::class, 'examplePage']);

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;

class ExampleController extends Controller{

    public function examplePage(){
        return $this->render('yourFileName');
    }
}


<h2>Hello World</h2>

use App\Http\Controllers\ExampleController;

$app->router->get('/hello', [ExampleController::class, 'examplePage']);

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;

class ExampleController extends Controller{

    public function examplePage(){
        return $this->render('yourFileName', [
            'text' => 'hello world'
        ]);
    }
}


<h2>  echo $text 

<div>
    <h2>Hello World</h2>
</div>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
</head>
<body>
    {{content}}
    <script src="assets/js/jquery-3.3.1.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
</body>
</html>

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;

class ExampleController extends Controller{
    public function examplePage(){
        $this->setLayout('yourLayoutName');
        return $this->render('yourFileName');
    }
}

use App\Http\Controllers\ExampleController;

$app->router->get('/hello/{id}', [ExampleController::class, 'examplePage']);

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Http\Request;

class ExampleController extends Controller{

    public function examplePage(Request $request){
        echo '<pre>';
        var_dump($request->getParams());
        echo '</pre>';
        return $this->render('yourFileName');
    }
}

 $form = \SmyPhp\Core\Form\Form::start('', 'post')

 $form = \SmyPhp\Core\Form\Form::start('/register', 'post')

use App\Http\Controllers\ExampleController;

$app->router->post('/register', [ExampleController::class, 'register']);

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Http\Request;
use App\Models\User;

class ExampleController extends Controller{

    public function register(Request $request){
        $this->setLayout('auth');
        $user = new User();
        //$user references the User model
        if($request->isPost()){
            //your registration logic comes here
            return $this->render('register', [
                'model' =>$user //this is the model being sent to the form in the register page
            ]);
        }
        return $this->render('register', [
            'model' =>$user //this is the model being sent to the form in the register page
        ]);
    }
}

 $form = \SmyPhp\Core\Form\Form::start('', 'post')

 $form = \SmyPhp\Core\Form\Form::start('/login', 'post')



namespace App\Models;

use SmyPhp\Core\DatabaseModel;
class User extends DatabaseModel
{
    //...

    public function labels(): array
    {
        return [
            'email' => 'Your Email',
            'password' => 'Your Password',
        ];
    }
}

use App\Models\User;
$user = (new User)->findOne(['email' => '[email protected]']); //finds row that email exists and returns only 1

/*
|--------------------------------------------------------------------------
| More than one conditions can be passed in
|
*/
$user = (new User)->findOne([
            'email' => '[email protected]',
            'id' => 2
        ]); //finds where row that email AND id exists

use App\Models\User;
$user = (new User)->findOneOrWhere([
        'email' => '[email protected]'
    ], ['id' => 2]); //finds where row that email OR id exists

use App\Models\User;
$user = (new User)->findAll(); //finds where row that email OR id exists

use App\Models\User;
$user = (new User)->findAllWhere([
        'email' => '[email protected]'
    ]); //finds ALL rows that email

use App\Models\User;
$user = (new User)->findAllOrWhere([
        'email' => '[email protected]'
    ], ['id' => 2]); //finds rows where email OR id exists

use App\Models\User;
$user = (new User)->count(); //returns the number of columns

use App\Models\User;
$user = (new User)->countWhere(['name'=>'john']); //returns the number of columns with name of john

use App\Models\User;
$user = (new User)->countOrWhere([
            'name'=>'john'
        ], [
            'status' => 1
        ]); //returns the number of columns with name of john or a status of 1

use App\Models\User;
$user = (new User)->delete([
            'name'=>'john'
        ]); //deletes the row(s) with name of john

use App\Models\User;
$user = (new User)->deleteOrWhere([
            'name'=>'john'
        ], [
            'email' => '[email protected]'
        ]); //deletes the row(s) with name of john or email of [email protected]

use App\Models\User;
$user = (new User)->update([
            'name'=>'john',
            'status'=> 1
        ], [
            'email' => '[email protected]'
        ]); //sets status to 1 and name to john where the email is [email protected]

use App\Models\User;
$user = (new User)->updateOrWhere([
            'name'=>'john',
            'status'=> 1
        ], [
            'email' => '[email protected]'
        ], [
            'id' => 4
        ]); //sets status to 1 and name to john where the email is [email protected] OR id is 4

use SmyPhp\Core\DatabaseModel;

$stmt = DatabaseModel::prepare("SELECT count(*) FROM users WHERE id = 2");
// $stmt->bindParam(); //this can be called if you are binding
$stmt->execute();

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use App\Http\Middleware\ApiMiddleware;

class ExampleController extends Controller{

    public function __construct(){
        $this->authenticatedMiddleware(new ApiMiddleware(['']));
    }
}

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use App\Http\Middleware\Authenticate;

class ExampleController extends Controller{

    public function __construct(){
        $this->authenticatedMiddleware(new Authenticate(['']));
    }
}

use SmyPhp\Core\Application;
if (!Application::$app->isGuest()) {
    Application::$app->response->redirect('/');
}

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use App\Providers\MailServiceProvider;

class ExampleController extends Controller{

    public function sendMail(){
        $subject = "subject";
        $email = "[email protected]";
        $name = "your name";
        $email_template = Application::$ROOT_DIR."/views/email.php"; //if the email will be sent in a template
        $send = (new MailServiceProvider)->Mail($subject, $email, $name, $email_template);
    }
}

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Application;

class ExampleController extends Controller{

    public function sendFlash(){
        Application::$app->session->setFlash('success', 'Thanks for joining');
        Application::$app->response->redirect('/'); //this redirects to the route where the flash message will appear
        exit;
    }
}

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use App\Providers\Image;

class ExampleController extends Controller{

    public function sendMail(){
        $base64Image = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEh"; 
        $path = Application::$ROOT_DIR."/routes/assets/uploads";
        $filename = "uploads_".uniqid().".jpg";
        $convertImage = Image::convert($base64Image, $path, $filename);
    }
}

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Http\Request;
use SmyPhp\Core\Http\Response;

class ExampleController extends Controller{

    public function sendResponse(Request $request, Response $response){
         return $response->json([
            "success" => false,
            "message" => "All fields are 

namespace App\Http\Controllers;
use SmyPhp\Core\Controller\Controller;
use SmyPhp\Core\Http\Request;
use SmyPhp\Core\Http\Response;
use SmyPhp\Core\Auth;

class ExampleController extends Controller{

    public function sendResponse(Request $request, Response $response){
        $user = Auth::User();
         return $response->json([
            "success" => false,
            "user" => $user
        ], 400);
    }
}
shell
$ php smyphp --start
shell
$ php smyphp --start --port 3344
shell
$ php smyphp --help
shell
$ php migrate.php