Build your first web app with DAF.
php v8.0 <-> v8.2 and set as environment variable.
15-20 minutes + download/installation time.
Create, use, and modify a
To start building DAF apps, download daf SDK zip.
After downloading, extract the zip file and add the daf app to the environment variables.
Once you've installed, open a new terminal and run the following command:
daf --version
If the installation succeeded, you should see version 1.0.0 or higher outputted:
1.0.0
If everything looks good, select the Continue button below to go to the next step.
"HelloWorldApp"
daf new
This command creates your new DAF Web App project and places it inside your current location. also use the folder name as the project name
use
project name
Take a quick look at the contents of the directory.
ls
dir
Several files were created in the directory, to give you a simple DAF app that is ready to run.
In your terminal, run the following command:
daf watch
The daf watch command will start the app, and then update the app whenever you make code changes. You can stop the app at any time by selecting Ctrl+C.
Wait for the app to display that it's listening on http://localhost: and for the browser to launch at that address.
Once you get to the following page, you have successfully run your first DAF app!
The displayed page is defined by the index.php file located inside the root directory. This is what its contents look like:
<?php namespace HelloWorldApp; require_once 'vendor/autoloader.php'; $app = new ApplicationEx(); $app->Router->Get("/" , "Pages/Home"); $app->Run();
It already contains the code that sets the homepage and displays the text "Hello world!."
"Hello world!."
The displayed page is defined by this line in the index.php file. for request with GET method with path / , it will render the {App Name}/Views/Pages/Home.php Component.
/
{App Name}/Views/Pages/Home.php
// minimal load $app->Router->Get("/" , "Pages/Home"); // or minimal load with endpoint before render $app->Router->Get("/" , fn(IViewManager $vm) => $vm->RenderView("Pages/Home")); $app->Router->Get("/" , function(IViewManager $vm) { return $vm->RenderView("Pages/Home"); }); // or load by register a controller.
daf g vc Components/NiceMsg
The command will create a new view component file located in {App Name}/Views/Components/NiceMsg.php.
{App Name}/Views/Components/NiceMsg.php
daf generate {type} {path} [options]
Types:
Here is the file content that generated:
<?php /** @var DafCore\Component $this */ ?> <h1>Hello From NiceMsg ViewComponent!</h1>
The top 3 lines are just for Component class intellisense.
Now edit the file content to:
<?php /** @var DafCore\Component $this */ $msg = $this->RequiredParameter("Text"); ?> <div class="alert alert-success"> <?=$msg?> </div>
the RequiredParameter("Text") function are make the component to require the "Text" Parameter.
RequiredParameter("Text")
Update Home Component file located in {App Name}/Views/Pages/Home.php to the following code.
<?php use HelloWorld\Views\Components; ?> <h1>Hello World!</h1> <NiceMsg Text="Hello From DAF Component :-)." />
the use keyword adds the namespace to the components namespaces.
Make sure the file is saved.!!!
This the content you need to see.
daf g vc Pages/About
The command will create a new view component file located in {App Name}/Views/Pages/About.php.
{App Name}/Views/Pages/About.php
<?php /** @var DafCore\Component $this */ ?> <h1>Hello From About ViewComponent!</h1>
Remove the top 3 lines we don't need them yet.
Now the file content need to look like this:
<h1>Hello From About ViewComponent!</h1>
Open the main index.php file and add the following code before $app->Run(); line.
index.php
$app->Run();
$app->Router->Get("/About", "Pages/About");
Or
// minimal load $app->Router->Get("/About", "Pages/About"); // or minimal load with endpoint before render $app->Router->Get("/About" , fn(IViewManager $vm) => $vm->RenderView("Pages/About")); $app->Router->Get("/About" , function(IViewManager $vm) { return $vm->RenderView("Pages/About"); });
Now manually navigate to /About to see the About page.
/About
daf g vc _Layouts/NavBar
The command will create a new view component file located in {App Name}/Views/_Layouts/NavBar.php.
{App Name}/Views/_Layouts/NavBar.php
Edit the file content that generated to this new content:
<nav> <a href="/">Home</a> <a href="/About">About</a> </nav>
Update MainLayout Component file located in {App Name}/Views/_Layouts/MainLayout.php to the following code.
{App Name}/Views/_Layouts/MainLayout.php
<?php use HelloWorld\Views\_Layouts; /** @var DafCore\Component $this */ ?> <main class="container-md pt-3"> <Navbar /> <?= $this->RenderChildContent() ?> </main>
One file to add usage for certain components that need to be available in all your view components.
daf g vc _GlobalUsing
The command will create a new view component file located in {App Name}/Views/_GlobalUsing.php.
{App Name}/Views/_GlobalUsing.php
Update _GlobalUsing Component content to the following code.
_GlobalUsing
<?php use HelloWorld\Views\_Layouts; use HelloWorld\Views\Components;
The * mark is for include all the components in the folder.
*
Update MainLayout Component content to the following code.
MainLayout
<main class="container-md pt-3"> <Navbar /> <?= $this->RenderChildContent() ?> </main>
Update Home Component content to the following code.
Home
<h1>Hello World!</h1> <NiceMsg Text="Hello From DAF Component :-)." />
Congratulations! You have successfully created your first DAF app.
In this tutorial, you have learned how to: