diff --git a/app/Http/Controllers/Backend/BlogController.php b/app/Http/Controllers/Backend/BlogController.php index a16cd56..9440cb4 100644 --- a/app/Http/Controllers/Backend/BlogController.php +++ b/app/Http/Controllers/Backend/BlogController.php @@ -4,10 +4,20 @@ use Illuminate\Http\Request; use App\Post; +use App\Http\Requests; +use Intervention\Image\Facades\Image; class BlogController extends BackendController { protected $limit=5; + protected $uploadPath; + + public function __construct() + { + parent::__construct(); + $this->uploadPath = public_path(config('cms.image.directory')); + } + /** * Display a listing of the resource. * @@ -26,9 +36,9 @@ public function index() * * @return \Illuminate\Http\Response */ - public function create() + public function create(Post $post) { - dd("create new blog"); + return view("backend.blog.create", compact('post')); } /** @@ -37,9 +47,44 @@ public function create() * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Requests\PostRequest $request) { - // + $data = $this->handleRequest($request); + + $request->user()->posts()->create($data); + + return redirect(route('backend.blog.index'))->with('message', 'Post has been added'); + } + + private function handleRequest($request){ + $data = $request->all(); + + if($request->hasFile('image')){ + $image = $request->file('image'); + $thumbnail = + $fileName = $image->getClientOriginalName(); + + $destination = $this->uploadPath; + + $uploaded = $image->move($destination, $fileName); + + if($uploaded){ + $width = config('cms.image.thumbnail.width'); + $height = config('cms.image.thumbnail.height'); + + $extension = $image->getClientOriginalExtension(); + $thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName); + + Image::make($destination . '/' . $fileName) + ->resize($width,$height) + ->save($destination . '/' . $thumbnail); + } + + + $data['image'] = $fileName; + } + + return $data; } /** diff --git a/app/Http/Requests/PostRequest.php b/app/Http/Requests/PostRequest.php new file mode 100644 index 0000000..1d35028 --- /dev/null +++ b/app/Http/Requests/PostRequest.php @@ -0,0 +1,35 @@ + 'required', + 'slug' => 'required|unique:posts', + 'body' => 'required', + 'published_at' => 'date_format:Y-m-d H:i:s', + 'category_id' => 'required', + 'image' => 'mimes:jpg,jpeg,bmp,png', + ]; + } +} diff --git a/app/Post.php b/app/Post.php index 897aa50..6b2592b 100644 --- a/app/Post.php +++ b/app/Post.php @@ -10,33 +10,39 @@ class Post extends Model { protected $dates = ['published_at']; - public function getImageUrlAttribute($value){ - - $imageUrl = ""; - if(!is_null($this->image)){ - $imageUrl = $this->image; - }else{ - $imageUrl = ""; - } - - return $imageUrl; - } - - public function getImageThumbUrlAttribute($value){ - - $imageUrl = ""; - if(!is_null($this->image)){ - $ext = substr(strrchr($this->image, '.'), 1); - $thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $this->image); - $imagePath = public_path() . "/img/" . $thumbnail; - if(file_exists($imagePath)) $imageUrl = asset("img/" . $thumbnail); - $imageUrl = $this->image; - }else{ - $imageUrl = ""; - } - - return $imageUrl; - } + protected $fillable = ['title','slug','excerpt','body','published_at','category_id','image']; + + public function getImageUrlAttribute($value){ + + $imageUrl = ""; + + if(!is_null($this->image)){ + $directory = config('cms.image.directory'); + $imagePath = public_path(). "/{$directory}/". $this->image; + + if(file_exists($imagePath)) $imageUrl = asset("{$directory}/" .$this->image); + } + + return $imageUrl; + } + + public function getImageThumbUrlAttribute($value){ + + $imageUrl = ""; + + if(!is_null($this->image)){ + $directory = config('cms.image.directory'); + $ext = substr(strrchr($this->image, '.'), 1); + $thumbnail = str_replace(".{$ext}", "_thumb.{$ext}", $this->image); + $imagePath = public_path() . "/{$directory}/" . $thumbnail; + if(file_exists($imagePath)) $imageUrl = asset("{$directory}/" . $thumbnail); + $imageUrl = $this->image; + }else{ + $imageUrl = ""; + } + + return $imageUrl; + } public function author(){ return $this->belongsTo(User::class); @@ -50,6 +56,10 @@ public function getDateAttribute($value){ return is_null($this->published_at) ? '' : $this->published_at->diffForHumans(); } + public function setPublishedAtAttribute($value){ + $this->attributes['published_at'] = $value ? : NULL; + } + public function getBodyHtmlAttribute($value){ return $this->body ? Markdown::convertToHtml(e($this->body)) : NULL ; } diff --git a/composer.json b/composer.json index 3e57d13..56bf760 100644 --- a/composer.json +++ b/composer.json @@ -8,8 +8,10 @@ "php": "^7.1.3", "fideloper/proxy": "^4.0", "graham-campbell/markdown": "^10.1", + "intervention/image": "^2.4", "laravel/framework": "5.7.*", - "laravel/tinker": "^1.0" + "laravel/tinker": "^1.0", + "laravelcollective/html": "^5.7" }, "require-dev": { "beyondcode/laravel-dump-server": "^1.0", diff --git a/composer.lock b/composer.lock index 3482208..41c8d24 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7b124efecb3e41483c4f58d3c5a2b760", + "content-hash": "428b6578222facf7b10e3e1cacda7e52", "packages": [ { "name": "dnoegel/php-xdg-base-dir", @@ -434,6 +434,141 @@ ], "time": "2018-08-23T12:03:07+00:00" }, + { + "name": "guzzlehttp/psr7", + "version": "1.4.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "reference": "f5b8a8512e2b58b0071a7280e39f14f72e05d87c", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Schultze", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "request", + "response", + "stream", + "uri", + "url" + ], + "time": "2017-03-20T17:10:46+00:00" + }, + { + "name": "intervention/image", + "version": "2.4.2", + "source": { + "type": "git", + "url": "https://github.com/Intervention/image.git", + "reference": "e82d274f786e3d4b866a59b173f42e716f0783eb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Intervention/image/zipball/e82d274f786e3d4b866a59b173f42e716f0783eb", + "reference": "e82d274f786e3d4b866a59b173f42e716f0783eb", + "shasum": "" + }, + "require": { + "ext-fileinfo": "*", + "guzzlehttp/psr7": "~1.1", + "php": ">=5.4.0" + }, + "require-dev": { + "mockery/mockery": "~0.9.2", + "phpunit/phpunit": "^4.8 || ^5.7" + }, + "suggest": { + "ext-gd": "to use GD library based image processing.", + "ext-imagick": "to use Imagick based image processing.", + "intervention/imagecache": "Caching extension for the Intervention Image library" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.4-dev" + }, + "laravel": { + "providers": [ + "Intervention\\Image\\ImageServiceProvider" + ], + "aliases": { + "Image": "Intervention\\Image\\Facades\\Image" + } + } + }, + "autoload": { + "psr-4": { + "Intervention\\Image\\": "src/Intervention/Image" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Oliver Vogel", + "email": "oliver@olivervogel.com", + "homepage": "http://olivervogel.com/" + } + ], + "description": "Image handling and manipulation library with support for Laravel integration", + "homepage": "http://image.intervention.io/", + "keywords": [ + "gd", + "image", + "imagick", + "laravel", + "thumbnail", + "watermark" + ], + "time": "2018-05-29T14:19:03+00:00" + }, { "name": "jakub-onderka/php-console-color", "version": "0.1", @@ -725,6 +860,74 @@ ], "time": "2018-05-17T13:42:07+00:00" }, + { + "name": "laravelcollective/html", + "version": "v5.7.1", + "source": { + "type": "git", + "url": "https://github.com/LaravelCollective/html.git", + "reference": "777b6d390811ba249255ed5750bf17a019cd88a5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/LaravelCollective/html/zipball/777b6d390811ba249255ed5750bf17a019cd88a5", + "reference": "777b6d390811ba249255ed5750bf17a019cd88a5", + "shasum": "" + }, + "require": { + "illuminate/http": "5.7.*", + "illuminate/routing": "5.7.*", + "illuminate/session": "5.7.*", + "illuminate/support": "5.7.*", + "illuminate/view": "5.7.*", + "php": ">=7.1.3" + }, + "require-dev": { + "illuminate/database": "5.7.*", + "mockery/mockery": "~1.0", + "phpunit/phpunit": "~7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.7-dev" + }, + "laravel": { + "providers": [ + "Collective\\Html\\HtmlServiceProvider" + ], + "aliases": { + "Form": "Collective\\Html\\FormFacade", + "Html": "Collective\\Html\\HtmlFacade" + } + } + }, + "autoload": { + "psr-4": { + "Collective\\Html\\": "src/" + }, + "files": [ + "src/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylorotwell@gmail.com" + }, + { + "name": "Adam Engebretson", + "email": "adam@laravelcollective.com" + } + ], + "description": "HTML and Form Builders for the Laravel Framework", + "homepage": "https://laravelcollective.com", + "time": "2018-09-05T18:32:53+00:00" + }, { "name": "league/commonmark", "version": "0.17.5", @@ -1156,6 +1359,56 @@ ], "time": "2017-02-14T16:28:37+00:00" }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06T14:39:51+00:00" + }, { "name": "psr/log", "version": "1.0.2", diff --git a/config/app.php b/config/app.php index 3b08339..810f807 100644 --- a/config/app.php +++ b/config/app.php @@ -151,6 +151,8 @@ * Package Service Providers... */ GrahamCampbell\Markdown\MarkdownServiceProvider::class, + Collective\Html\HtmlServiceProvider::class, + Intervention\Image\ImageServiceProvider::class, /* * Application Service Providers... @@ -211,6 +213,10 @@ 'Validator' => Illuminate\Support\Facades\Validator::class, 'View' => Illuminate\Support\Facades\View::class, 'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class, + + 'Form' => Collective\Html\FormFacade::class, + 'Html' => Collective\Html\HtmlFacade::class, + 'Image' => Intervention\Image\Facades\Image::class, ], ]; diff --git a/config/cms.php b/config/cms.php new file mode 100644 index 0000000..e56156f --- /dev/null +++ b/config/cms.php @@ -0,0 +1,11 @@ + [ + 'directory' => 'img', + 'thumbnail' => [ + 'width' => 250, + 'height' => 170 + ] + ] +]; \ No newline at end of file diff --git a/config/image.php b/config/image.php new file mode 100644 index 0000000..2b1d2c3 --- /dev/null +++ b/config/image.php @@ -0,0 +1,20 @@ + 'gd' + +]; diff --git a/public/img/comb.jpg b/public/img/comb.jpg new file mode 100644 index 0000000..faab1fc Binary files /dev/null and b/public/img/comb.jpg differ diff --git a/public/img/comb_thumb.jpg b/public/img/comb_thumb.jpg new file mode 100644 index 0000000..9fd1df8 Binary files /dev/null and b/public/img/comb_thumb.jpg differ diff --git a/resources/views/backend/blog/create.blade.php b/resources/views/backend/blog/create.blade.php new file mode 100644 index 0000000..94d3b63 --- /dev/null +++ b/resources/views/backend/blog/create.blade.php @@ -0,0 +1,138 @@ +@extends('layouts.backend.main') + +@section('title', 'Laravel 5 Blog | Add New POst') + +@section('content') +
+ +
+
+
+
+

Blog Add New Posts

+
+
+ +
+
+
+
+ +
+
+
+
+
+ +
+

Add New Post

+
+
+ + +
+ +
+
+
+
+ + + +
+ {!! Form::model($post, [ + 'method' => 'POST', + 'route' => 'backend.blog.store', + 'files' => TRUE, + ])!!} + +
+ {!! Form::label('title') !!} + {!! Form::text('title', null, ['class'=>'form-control']) !!} + @if($errors->has('title')) + {{ $errors->first('title') }} + @endif +
+ +
+ {!! Form::label('slug') !!} + {!! Form::text('slug', null, ['class'=>'form-control']) !!} + @if($errors->has('slug')) + {{ $errors->first('slug') }} + @endif +
+ +
+ {!! Form::label('excerpt') !!} + {!! Form::textarea('excerpt', null, ['class'=>'form-control']) !!} + @if($errors->has('excerpt')) + {{ $errors->first('excerpt') }} + @endif +
+ +
+ {!! Form::label('body') !!} + {!! Form::textarea('body', null, ['class'=>'form-control']) !!} + @if($errors->has('body')) + {{ $errors->first('body') }} + @endif +
+ +
+ {!! Form::label('published_at', 'Publish Date') !!} + {!! Form::text('published_at', null, ['class'=>'form-control','placeholder' => 'Date Format: Y-m-d H:i:s']) !!} + @if($errors->has('published_at')) + {{ $errors->first('published_at') }} + @endif +
+ +
+ {!! Form::label('category_id', 'Category') !!} + {!! Form::select('category_id', App\Category::pluck('title','id'), null, ['class'=>'form-control', 'placeholder' => 'Select Category']) !!} + @if($errors->has('category_id')) + {{ $errors->first('category_id') }} + @endif +
+ +
+ {!! Form::label('image', 'Featured Image') !!} +
+ {!! Form::file('image') !!} + + @if($errors->has('image')) + {{ $errors->first('image') }} + @endif +
+ +
+ + {!! Form::submit('Create New Post', ['class' => 'btn btn-primary']) !!} + + {!! Form::close() !!} +
+ + +
+ +
+ +
+ +
+
+ +
+ +@endsection + +@section('script') + +@endsection