PHP code example of dirkblack / blog

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

    

dirkblack / blog example snippets


use DirkBlack\Blog\Support\BlogRoleResolver;

// In tinker or a seeder:
BlogRoleResolver::assign($user, 'admin');

use DirkBlack\Blog\Traits\HasBlogRole;

class User extends Authenticatable
{
    use HasBlogRole;
}

// Then:
$user->assignBlogRole('admin');

'prefix' => 'blog',

'middleware' => [
    'web' => ['web'],       // Applied to the SPA catch-all route
    'api' => ['web'],       // Applied to all API routes
],

'user_model' => 'App\\Models\\User',

'comments' => [
    'enabled' => true,        // Master switch: turn commenting on/off
    'auto_approve' => false,  // true = publish instantly; false = hold as pending for admin approval
    'threading_depth' => 3,   // Max nesting depth (0 = flat comments)
],

'media' => [
    'disk' => env('BLOG_MEDIA_DISK', 'public'),
    'collection' => 'blog-media',
],

'notifications' => [
    'post_published' => false,  // Email author when scheduled post auto-publishes
],

'load_default_css' => true,

'footnotes' => [
    'backref_class'      => 'footnote-backref',
    'backref_symbol'     => '↩',
    'container_add_hr'   => true,
    'container_class'    => 'footnotes',
    'ref_class'          => 'footnote-ref',
    'ref_id_prefix'      => 'fnref:',
    'footnote_class'     => 'footnote',
    'footnote_id_prefix' => 'fn:',
],

'markdown_classes' => [
    'h1' => 'text-4xl font-bold mb-6',
    'h2' => 'text-2xl font-semibold mb-4 mt-8',
    'p'  => 'mb-4 leading-relaxed',
    // ... see config file for all 12 elements
],

use DirkBlack\Blog\Traits\HasBlogRole;

class User extends Authenticatable
{
    use HasBlogRole;
}

$user->blogRole();           // Returns BlogRole enum (e.g., BlogRole::Author)
$user->assignBlogRole('author');
$user->revokeBlogRole();    // Reverts to guest
$user->hasBlogRole('author'); // true if author or higher
$user->isBlogAdmin();       // true if admin
$user->isBlogAuthor();      // true if author or admin

use DirkBlack\Blog\Support\BlogRoleResolver;

BlogRoleResolver::resolve($user);          // Returns BlogRole enum
BlogRoleResolver::assign($user, 'admin');  // Assign role
BlogRoleResolver::revoke($user);           // Remove role (back to guest)

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
}

// config/blog.php
'markdown_classes' => [
    'h2'         => 'text-2xl font-semibold mb-4 mt-8',
    'p'          => 'mb-4 leading-relaxed',
    'blockquote' => 'border-l-4 border-gray-300 pl-4 italic',
    // ...
],

$post = Post::create([
    'title' => 'My Post',
    'slug' => 'my-post',
    'body_markdown' => '# Hello **world**',
    // body_html is auto-populated
]);

use DirkBlack\Blog\Support\Markdown\MarkdownConverter;

$converter = app(MarkdownConverter::class);
$html = $converter->convert('Some **markdown** text');

// Via API: POST /author/posts/{id}/publish
{ "scheduled_at": "2026-04-01T09:00:00" }
bash
# Publish compiled frontend assets (ssets

# Publish config file (recommended)
php artisan vendor:publish --tag=blog-config

# Publish migrations if you want to customize them
php artisan vendor:publish --tag=blog-migrations
bash
php artisan migrate
bash
php artisan vendor:publish --tag=blog-styles