wink icon indicating copy to clipboard operation
wink copied to clipboard

feature: wink:controller command that creates a sample controller to display posts

Open Leamsi9 opened this issue 3 years ago • 0 comments

Summary

php artisan wink:controller {name} can be run alongside wink:install and wink:migrate to provide boilerplate to display wink posts.

Implementation

  • I created a console command to generate the new controller file in the main Http/Controllers directory, with correct filename, class and namespacing.
  • I created a Stubs directory, and provided the required BlogController stub
  • I updated the wink service provider with the new command.

Use

You can generate a sample controller by running:

php artisan wink:controller BlogController

This will create a file in your Http/Controllers directory named BlogController.php. You can replace BlogController in the commandnoun you prefer which will prefix the file and class names (e.g. if you usephp artisan wink:controller Post the file and class created will be named PostController with any name for your controller.

The sample controller contains the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Wink\WinkPost;

class BlogController extends Controller
{
    public function index()
    {
        $posts = WinkPost::with('tags')
            ->live()
            ->orderBy('publish_date', 'DESC')
            ->simplePaginate(10);
        return view('blog.index', [
            'posts' => $posts,
        ]);
    }

    public function show ($slug)
    {
        $post = WinkPost::live()->whereSlug($slug)->firstOrFail();

        return view('post.index', [
            'post' => $post
        ]);
    }
}

This mean the index() method will retrieve 10 published blog posts per page with their associated tags, most recent first, and render them in the blog.index view. You will need to create that view and the associated route.

The show() method in turn will retrieve a single published post corresponding to its slug and render it in the post.index view, displaying an error message if it's not found. You will need to create that view with the associated route,.

Leamsi9 avatar Jan 09 '21 03:01 Leamsi9