Skip to content

Getting Started

Start a New MVC

Run

shell
php windwalker g view Front/Sakura

This will create a src/Front/Sakura/SakuraView file and a series of files.

Let's write something into the prepare() function.

php
<?php
// src/Front/Sakura/SakuraView.php

// ...

class SakuraView implements ViewModelInterface
{
    // ...
    
    public function prepare(AppContext $app, View $view): array
    {
        $title = 'Sakura View Title'; 
        
        return []; 
        return compact('title'); 
    }
}

Then print out the variable in the template file.

blade
<?php // src/Module/Front/Sakura/views/sakura.blade.php ?>

@extends('global.body')

@section('content')
    <h2>Sakura view</h2>
    <div class="container">
        <h2>{{ $title }}</h2>
    </div>
@stop

Next, run the following command to create routing.

shell
php windwalker g route front/sakura

Edit the file to add the route settings.

php
<?php
// routes/front/sakura.route.php

use App\Module\Front\Sakura\SakuraView; 

// ...

$router->group('sakura')
    ->register(function (RouteCreator $router) {
        // Add this
        $router->any('sakura', '/sakura')  
            ->view(SakuraView::class);  
    });

Next, open the following URL in the browser to see the view we create.

http://localhost:8000/sakura

sakura title

Modify CSS Styles

Next, let's try modifying the assets/_sakura.scss in sakura. Add:

scss
// src/Module/Front/Sakura/assets/_sakura.scss

// Limit to this view only
.view-sakura {
  h2 {
    color: red;
  }
}

Next, run

yarn build css

After completion, you will see the style take effect.

red title

In Windwalker 4, CSS is distributed in different MVC folders and will be integrated together during fusion compile.

Modify JS

Similar to CSS, JS is also distributed in the MVC folders corresponding to the view name. Let's modify sakura.ts.

ts
// src/Module/Front/Sakura/assets/sakura.ts

console.log('Hello Sakura');

Then compile with fusion.

yarn build js

Refresh the page and open the browser console to see the result.

console

Page Title

Simply add this line to add title to browser:

php
    // ...

    public function prepare(AppContext $app, View $view): array
    {
        $title = 'Sakura View Title';

        $view->setTitle($title); 

        return compact('title');
    }

title

Use #[ViewMetadata] Attribute

If you want to set more metadata, try use #[ViewMetadata] with another method. You amy inject any service or view data into this method through declaring the arguments.

The HtmlFrame is a useful object that help you configure HTML header and body. See HtmlFrame Service

php
use Windwalker\Core\Attributes\ViewMetadata; 
use Windwalker\Core\Html\HtmlFrame; 

    // ...

    public function prepare(AppContext $app, View $view): array
    {
        $title = 'Sakura View Title';

        $view->setTitle($title); 
        // Inject title to view
        $view['title'] = $title; 

        return compact('title');
    }

    #[ViewMetadata] 
    public function prepareMetadata(HtmlFrame $htmlFrame, string $title): void
    { 
        $htmlFrame->setTitle($title); 
        $htmlFrame->setDescription('Sakura Description'); 
    } 

Debug Mode

If you need to enter debug mode, such as viewing DB Queries, complete error messages, or want to prevent CSS/JS caching, you can enter debug mode.

The simplest way is to add /dev.php at the end of the website URL.

For example:

In this case, we are using PHP server: http://localhost:8000, we can simply add dev.php after ths host: http://localhost:8000/dev.php

This will enter debug mode.

debug console

When an error occurs, it can also display complete error messages.

error page

More details please see Debugging

Released under the MIT License.