Skip to content

Create a blog

SilverEngine edited this page Oct 7, 2018 · 19 revisions

Create a blog from scratch with SE

1. Prepare envirement

1.1 local env

Let's set up local env for our new database table. Open local.env.php and edit configuration to your needs.

   "local"   => [
       'service'       => true,
       'driver'        => 'mysql',
       'hostname'      => 'localhost',
       'username'      => 'silver',
       'password'      => 'secret',
       'basename'      => 'youtdatabase',
       'limit_request' => 25,
    ]

2. Create resources and DB

2.1 Resources

Open console window inside your project and type:

php silver g resources news

------- OR --------

2.1 Create controller

Open console window inside your project and type:

php silver g controller news

this command will prepare for you new App\Conrollers\NewsController.php and add new line inside App\Route.php.

2.2 Models

For create new model use command:

php silver g model news

This command will prepare for you new model called App\Models\News.php

2.3 Migrations (bug to fix - please make manualy)

For create new migration use command:

php silver g migrations news

This command will prepare for you new migraion file in Database\Migrations\NewsMigrations.php. Please copy this and replace the default layout of the migration file.

      Query::create(static::$table, function($q) {
           $q->integer('id')->primary()->autoincrement();

           $q->varchar("title", 255);
           $q->varchar("seo_title", 255);
           $q->text("short_description")->nullable();
           $q->text("description")->nullable();
           $q->integer('active')->default(0);

           $q->datetime("create_at", 255)->default(new Raw('CURRENT_TIMESTAMP'));
           $q->datetime("update_at", 255)->nullable();
           $q->datetime("delete_at", 255)->nullable();
       })->execute();

2.4 Seed (bug to fix - please make manualy)

For create new seed use command:

php silver g seed news

This command will prepare for you new seed file in Database\Seed\NewsSeed.php. Please copy this and replace the default layout of the seed file.

       Seed::insert(static::$table, [
           'title'             => 'My first blog post',
           'seo_title'         => 'my-first-blog-post',
           'short_description' => 'it works!',
           'description'       => 'My first post works!',
           'active'            => 1,
       ])->execute();

2.5 Run migration and seed (bug to fix - please make manualy)

php silver migrate -seed

3 Backend basic

3.1 Prepare route for dynamical input

Open file inside App\Route.php and edit:

// Route for News controller.
Route::get('/news/{id?}', 'News@get', 'unguard');

unguard is by default Middleware to allow guest view the page.

{id?} => get dynamical parameter from url.

? => Escape string/segments after that parameter.

3.2 Prepare controller for the view

Open file inside App\Controllers\NewsController.php and edit:

namespace App\Controllers;

use App\Models\News;
use Silver\Core\Controller;
use Silver\Http\View;

/**
* News controller
*/
class NewsController extends Controller
{
   public function get($id = false)
   {
       // get from browser id via route
       if ($id) {
           return View::make('news.single')->with('news', News::find($id)->fetch());
       } else {
           return View::make('news.all')->with('news', News::all());
       }
   }

   public function post()
   {
       echo 'Methode: post';
   }

   public function put()
   {
       echo 'Methode: put';
   }

   public function delete()
   {
       echo 'Methode: delete';
   }
}

3.2 Prepare model

Open file inside App\Models\News.php and edit:

class News extends Model
{

   protected $_table = 'news';
   
   protected $hidden = [];
}

4 Make view

4.1 Master layout with Ghost Template engine

Open file inside App\Views\layouts\master.ghost.php and edit html to you need in our tutorial we make simple bootstrap theme:

<!DOCTYPE html>
<html lang="en">
<head>
 <title>{{ lang('master.title') ?: 'SilverEngine - blog'}}</title>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 <link rel="stylesheet" href="{{ asset('css/theme-style.css') }}">
</head>
<body>

 <div class="container">
    #block(content)
 </div>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 <script src="{{ asset('js/app.js') }}"></script>
</body>
</html>

4.2 News html multi

Open file inside App\Views\news\all.ghost.php and edit html to you need in our tutorial we make:

 <div class="row">
     <div class="col-md-8">
         #foreach($news as $article)
            <div class="col-md-12">
               <h1>{{ $article->title }}</h1>
               <p>{{ $article->short_description }}</p>
            </div>
         #endforeach
     </div>
     <div class="col-md-4">
        {{ include('includes.sidebar') }}
     </div>
 </div>

4.2 News html single

Open file inside App\Views\news\single.ghost.php and edit html to you need in our tutorial we make:

 <div class="row">
     <div class="col-md-8">
            <div class="col-md-12">
               <h1>{{ $news->title }}</h1>
               <p>{{ $news->short_description }}</p>
            </div>
            <div class="col-md-12">
              <!-- will be added to next patch  -->
              {{ component('news.comments') }}
            </div>
     </div>
     <div class="col-md-4">
        {{ include('includes.sidebar') }}
     </div>
 </div>

4.3 Including the Sidebar

Open file or create inside App\Views\includes\navbar.ghost.php and edit html to you need in our tutorial we make:

 <ul class="row">
     <li">
         <a href="{{ url('/') }}">{{ lang('navbar.home') }}</a>
     </li>
     <li">
         <a href="{{ route:news }}">{{ lang('navbar.news') }}</a>
     </li>
 </ul>

4.4 components

Open file or create inside App\Views\components\comments.ghost.php and edit html to you need in our tutorial we make:

<div class="row">
     <div class="col-md-12">
         #foreach($comments as $comment)
            <div class="col-md-12">
               <h3>{{ $comment->username }}</h3>
               <p>{{ $comment->description }}</p>
            </div>
         #endforeach
     </div>
     <div class="col-md-12">
        {{ include('includes.comment_form') }}
     </div>
 </div>

5. Adding new posts

5.1 Prepare controller for the view

Open file inside App\Controllers\NewsController.php and edit:

namespace App\Controllers;

use App\Models\News;
use Silver\Core\Controller;
use Silver\Http\View;
use Silver\Database\Query;
use Silver\Http\Validator;
use Silver\Core\Bootstrap\Facades\Request;
use Silver\Facades\Auth;
use Silver\Facades\SEO;

/**
* News controller
*/
class NewsController extends Controller
{
   private $table = 'news';

   public function get($id = false)
   {
       // get from browser id via route
       if ($id) {
           return View::make('news.single')->with('news', News::find($id)->fetch());
       } else {
           return View::make('news.all')->with('news', News::all());
       }
   }

   public function post()
   {
       $req = Request::all();
       
        $validator = Validator::check($req, [
           "title" => "min:3|required",
           "description"    => "min:3",
       ]);

       if (Validator::pass()) {
           Query::insert($this->table, [
               'id_user'      => Auth::me()->id,
               'title'        => Request::input('title'),
               'seo_title'    => SEO::url(Request::input('title')),
               'description'  => Request::input('description'),
           ])->execute();

           Redirect::to('/admin/news/all');
       } else {
           Session::flash('error', $validator);
           Redirect::to('/admin/news/add');
       }
   }

   public function put()
   {
       $validator = Validator::check(Request::all(), [
           "id"   => "min:1|required",
           "name" => "min:1|required",
           "description" => "min:1",
       ]);

       $req = (object) Request::all();

       if (Validator::pass()) {

           $data = [
               'title' => $req->title,
               'description'           => $req->description,
           ];

           Query::update($this->table, $data)->where('id', $req->id)->execute();

           Redirect::to('/admin/news/all');
       } else {
           Session::flash('error', $validator);
           Redirect::to('/admin/news/add');
       }
   }

   public function delete()
   {
       $validator = Validator::check(Request::all(), [
           "id"   => "min:1|required",
       ]);

       $req = (object) Request::all();

       if (Validator::pass()) {

           Query::delete()->from($this->table)->where('id', $req->id)->execute();

           Redirect::to('/admin/news/all');
       } else {
           Session::flash('error', $validator);
           Redirect::to('/admin/news');
       }
   }
}

6. Using and working with Helpers

For create new helper use command:

php silver g helper SEO

This will create for you in App\Helpers\SEO.php

namespace App\Helpers;

class SEO {
   public function help()
   {
       return "your code";
   }
}

7. Using and working with facades

For create new helper use command:

This will create for you in App\Facades\SEO.php

namespace App\Facades;

use Silver\Support\Facade;

class SEO extends Facade
{
   protected static function getClass()
   {
       return 'App\Helpers\SEO';
   }
}