PHP code example of dasunnethsara / zenithphp

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

    

dasunnethsara / zenithphp example snippets


    use ZenithPHP\Core\Http\Router;
    Router::get('/hello', 'WelcomeController', 'index');
    

    use ZenithPHP\Core\Controller\Controller;

    class WelcomeController extends Controller
    {
        public function index()
        {
            $this->view('welcome');
        }
    }
    

    use ZenithPHP\Core\Http\Router;
    Router::get('/api/users', 'UserController', 'index');
    

    use ZenithPHP\Core\Model\Model;

    class User extends Model
    {
        protected string $table_name = 'users';
    }
    

    use ZenithPHP\Core\Controller\Controller;
    use ZenithPHP\Core\Http\Response;
    use ZenithPHP\Core\Http\Request;
    use ZenithPHP\App\Models\User;

    class UserController extends Controller
    {
        public function index(Request $request, Response $response)
        {
            $user = new User($this->pdo);
            $allUsers = $user->getAll();
            $response->json(['status' => 'success', 'data' => $allUsers]);
        }
    }
    

class HomeController extends Controller 
{
    public function index(Request $request, Response $response)
    {
        return view('home', [
            'username' => 'John Doe',
            'userRole' => 'admin',
            'items' => [
                (object) ['name' => 'Laptop', 'price' => '$999'],
                (object) ['name' => 'Smartphone', 'price' => '$499'],
            ]
        ]);
    }
}

<!-- Displaying a variable -->
<< $username >>

<!-- Conditional Statement -->
@if ($userRole == 'admin')
  <p>Welcome, Admin!</p>
@elseif ($userRole == 'member')
  <p>Welcome, Member!</p>
@else
  <p>Welcome, Guest!</p>
@endif

<!-- Looping Through Data -->
@foreach ($items as $item)
  <p><< $item->name >> - << $item->price >></p>
@endforeach
bash
    php cli run
    
bash
    php cli make:model User
    
bash
    php cli make:controller UserController