Create a blog
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,
]
Open console window inside your project and type:
php silver g resources news
------- OR --------
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.
For create new model use command:
php silver g model news
This command will prepare for you new model called App\Models\News.php
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();
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();
php silver migrate -seed
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.
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';
}
}
Open file inside App\Models\News.php and edit:
class News extends Model
{
protected $_table = 'news';
protected $hidden = [];
}
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>
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>
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>
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>
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>
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');
}
}
}
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";
}
}
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';
}
}