diff --git a/README.md b/README.md index 3ed385a..ab02ae5 100755 --- a/README.md +++ b/README.md @@ -1,66 +1,36 @@ -

Laravel Logo

-

-Build Status -Total Downloads -Latest Stable Version -License -

+# Simple Laravel 9 Blog App +### Post, Comment and Authentication -## About Laravel +# Installation steps -Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as: +### 1. Clone the repository +```` +git clone https://github.com/deva7mad/simple-blog.git +```` -- [Simple, fast routing engine](https://laravel.com/docs/routing). -- [Powerful dependency injection container](https://laravel.com/docs/container). -- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage. -- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent). -- Database agnostic [schema migrations](https://laravel.com/docs/migrations). -- [Robust background job processing](https://laravel.com/docs/queues). -- [Real-time event broadcasting](https://laravel.com/docs/broadcasting). +### 2. Configure the database connection +```` +cd simple-blog/ -Laravel is accessible, powerful, and provides tools required for large, robust applications. +cp .env.example .env +```` +#### Then Add your database config to .env -## Learning Laravel +### 3. Running Migration +```` +php artisan migrate --seed +```` +#### This will see the database with 30 posts and for each post 5 comments each comment associated with a user. -Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework. +### 4. Storage Link +```` +php artisan storage:link +```` -You may also try the [Laravel Bootcamp](https://bootcamp.laravel.com), where you will be guided through building a modern Laravel application from scratch. +### 5. Run the app +```` +php artisan serve +```` +#### You can access dashboard with any generated user email and password will be (password) -If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains over 2000 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library. - -## Laravel Sponsors - -We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel [Patreon page](https://patreon.com/taylorotwell). - -### Premium Partners - -- **[Vehikl](https://vehikl.com/)** -- **[Tighten Co.](https://tighten.co)** -- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)** -- **[64 Robots](https://64robots.com)** -- **[Cubet Techno Labs](https://cubettech.com)** -- **[Cyber-Duck](https://cyber-duck.co.uk)** -- **[Many](https://www.many.co.uk)** -- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)** -- **[DevSquad](https://devsquad.com)** -- **[Curotec](https://www.curotec.com/services/technologies/laravel/)** -- **[OP.GG](https://op.gg)** -- **[WebReinvent](https://webreinvent.com/?utm_source=laravel&utm_medium=github&utm_campaign=patreon-sponsors)** -- **[Lendio](https://lendio.com)** - -## Contributing - -Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions). - -## Code of Conduct - -In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct). - -## Security Vulnerabilities - -If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed. - -## License - -The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index c1bf131..e4488a7 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -2,85 +2,38 @@ namespace App\Http\Controllers; -use App\Http\Requests\StoreCommentRequest; -use App\Http\Requests\UpdateCommentRequest; +use App\Models\Post; use App\Models\Comment; +use Illuminate\Http\RedirectResponse; +use App\Http\Requests\StoreCommentRequest; class CommentController extends Controller { /** - * Display a listing of the resource. - * - * @return \Illuminate\Http\Response - */ - public function index() - { - // - } - - /** - * Show the form for creating a new resource. + * Create a new controller instance. * - * @return \Illuminate\Http\Response + * @return void */ - public function create() + public function __construct() { - // + $this->middleware('auth'); } /** * Store a newly created resource in storage. * - * @param \App\Http\Requests\StoreCommentRequest $request - * @return \Illuminate\Http\Response + * @param StoreCommentRequest $request + * @param Post $post + * @return RedirectResponse */ - public function store(StoreCommentRequest $request) + public function store(StoreCommentRequest $request, Post $post): RedirectResponse { - // - } - - /** - * Display the specified resource. - * - * @param \App\Models\Comment $comment - * @return \Illuminate\Http\Response - */ - public function show(Comment $comment) - { - // - } + $data = $request->validated(); + $comment = Comment::make($data); + $comment->post()->associate($post); + $comment->user()->associate(auth()->user()); + $comment->save(); - /** - * Show the form for editing the specified resource. - * - * @param \App\Models\Comment $comment - * @return \Illuminate\Http\Response - */ - public function edit(Comment $comment) - { - // - } - - /** - * Update the specified resource in storage. - * - * @param \App\Http\Requests\UpdateCommentRequest $request - * @param \App\Models\Comment $comment - * @return \Illuminate\Http\Response - */ - public function update(UpdateCommentRequest $request, Comment $comment) - { - // - } - - /** - * Remove the specified resource from storage. - * - * @param \App\Models\Comment $comment - * @return \Illuminate\Http\Response - */ - public function destroy(Comment $comment) - { - // + return redirect()->back()->with('saved', 'Comment saved.'); } } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 7cbc2c3..a5f83e5 100755 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -2,7 +2,8 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use App\Models\Post; +use Illuminate\Contracts\Support\Renderable; class HomeController extends Controller { @@ -13,15 +14,26 @@ class HomeController extends Controller */ public function __construct() { - $this->middleware('auth'); + $this->middleware('auth')->except('welcome'); } /** * Show the application dashboard. * - * @return \Illuminate\Contracts\Support\Renderable + * @return Renderable */ - public function index() + public function welcome(): Renderable + { + $posts = Post::latest()->take(6)->get(); + return view('welcome', compact('posts')); + } + + /** + * Show the application dashboard. + * + * @return Renderable + */ + public function index(): Renderable { return view('home'); } diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 2f3b762..a03080f 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -2,85 +2,129 @@ namespace App\Http\Controllers; +use App\Libs\Uploader; +use App\Models\Post; +use Exception; +use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Response; +use Illuminate\Contracts\View\View; +use Illuminate\Contracts\View\Factory; use App\Http\Requests\StorePostRequest; use App\Http\Requests\UpdatePostRequest; -use App\Models\Post; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Support\Facades\Storage; class PostController extends Controller { + /** + * Create a new controller instance. + * + * @return void + */ + public function __construct() + { + $this->middleware('auth')->except('show'); + } + /** * Display a listing of the resource. * - * @return \Illuminate\Http\Response + * @return Application|Factory|View */ - public function index() + public function index(): Application|Factory|View { - // + $posts = Post::latest()->simplePaginate(10); + return view('posts.index', compact('posts')); } /** * Show the form for creating a new resource. * - * @return \Illuminate\Http\Response + * @return Application|Factory|View */ - public function create() + public function create(): Application|Factory|View { - // + return view('posts.create'); } /** * Store a newly created resource in storage. * - * @param \App\Http\Requests\StorePostRequest $request - * @return \Illuminate\Http\Response + * @param StorePostRequest $request + * @return RedirectResponse + * @throws Exception */ - public function store(StorePostRequest $request) + public function store(StorePostRequest $request): RedirectResponse { - // + $data = $request->validated(); + $data['image'] = Uploader::upload($data['image']); + $post = Post::make($data); + $post->save(); + + return redirect()->route('posts.index')->with('saved', 'New Post Saved'); + } /** * Display the specified resource. * - * @param \App\Models\Post $post - * @return \Illuminate\Http\Response + * @param Post $post + * @return Application|Factory|View */ - public function show(Post $post) + public function show(Post $post): Application|Factory|View { - // + return view('posts.show', compact('post')); } /** * Show the form for editing the specified resource. * - * @param \App\Models\Post $post - * @return \Illuminate\Http\Response + * @param Post $post + * @return Application|Factory|View */ - public function edit(Post $post) + public function edit(Post $post): Application|Factory|View { - // + return view('posts.edit', compact('post')); } /** * Update the specified resource in storage. * - * @param \App\Http\Requests\UpdatePostRequest $request - * @param \App\Models\Post $post - * @return \Illuminate\Http\Response + * @param UpdatePostRequest $request + * @param Post $post + * @return RedirectResponse + * @throws Exception */ - public function update(UpdatePostRequest $request, Post $post) + public function update(UpdatePostRequest $request, Post $post): RedirectResponse { - // + $data = $request->validated(); + + if ($request->has('image')) { + Storage::delete(asset('images/'. $post->image)); + $data['image'] = Uploader::upload($data['image']); + } + + $post->update($data); + + return redirect()->route('posts.index')->with('saved', 'Post Updated'); + } /** * Remove the specified resource from storage. * - * @param \App\Models\Post $post - * @return \Illuminate\Http\Response + * @param Post $post + * @return RedirectResponse */ - public function destroy(Post $post) + public function destroy(Post $post): RedirectResponse { - // + if ($post->image) { + Storage::delete(asset('images/'. $post->image)); + } + + $post->comments()->delete(); + $post->delete(); + + return redirect()->route('posts.index')->with('saved', 'Post Deleted'); } } diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php index a2813a0..0d09300 100755 --- a/app/Http/Middleware/RedirectIfAuthenticated.php +++ b/app/Http/Middleware/RedirectIfAuthenticated.php @@ -2,22 +2,24 @@ namespace App\Http\Middleware; -use App\Providers\RouteServiceProvider; use Closure; use Illuminate\Http\Request; +use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; +use App\Providers\RouteServiceProvider; class RedirectIfAuthenticated { /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next + * @param Request $request + * @param Closure(Request): (Response|RedirectResponse) $next * @param string|null ...$guards - * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse + * @return Response|RedirectResponse */ - public function handle(Request $request, Closure $next, ...$guards) + public function handle(Request $request, Closure $next, ...$guards): Response|RedirectResponse { $guards = empty($guards) ? [null] : $guards; diff --git a/app/Http/Requests/StoreCommentRequest.php b/app/Http/Requests/StoreCommentRequest.php index 16dc5c1..aa763cb 100644 --- a/app/Http/Requests/StoreCommentRequest.php +++ b/app/Http/Requests/StoreCommentRequest.php @@ -11,9 +11,9 @@ class StoreCommentRequest extends FormRequest * * @return bool */ - public function authorize() + public function authorize(): bool { - return false; + return true; } /** @@ -21,10 +21,11 @@ public function authorize() * * @return array */ - public function rules() + public function rules(): array { return [ - // + 'body' => 'required|min:10|regex:/^[\pL\s\-]+$/u', +// 'post_id' => 'required|exists:posts,id' ]; } } diff --git a/app/Http/Requests/StorePostRequest.php b/app/Http/Requests/StorePostRequest.php index d0ed11e..fd2bafa 100644 --- a/app/Http/Requests/StorePostRequest.php +++ b/app/Http/Requests/StorePostRequest.php @@ -11,9 +11,9 @@ class StorePostRequest extends FormRequest * * @return bool */ - public function authorize() + public function authorize(): bool { - return false; + return true; } /** @@ -21,10 +21,12 @@ public function authorize() * * @return array */ - public function rules() + public function rules(): array { return [ - // + 'title' => ['required', 'unique:posts', 'alpha', 'min:10', 'max:250'], + 'image' => ['required', 'image', 'mimes:jpeg,png,jpg,webp', 'max:2048'], + 'content' => ['required', 'min:20'], ]; } } diff --git a/app/Http/Requests/UpdatePostRequest.php b/app/Http/Requests/UpdatePostRequest.php index 3bf324d..10fcf21 100644 --- a/app/Http/Requests/UpdatePostRequest.php +++ b/app/Http/Requests/UpdatePostRequest.php @@ -11,9 +11,9 @@ class UpdatePostRequest extends FormRequest * * @return bool */ - public function authorize() + public function authorize(): bool { - return false; + return true; } /** @@ -21,10 +21,12 @@ public function authorize() * * @return array */ - public function rules() + public function rules(): array { return [ - // + 'title' => 'required|alpha|min:10|max:250|unique:posts,title,'.request()->post->id, + 'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:2048'], + 'content' => ['required', 'min:20'], ]; } } diff --git a/app/Libs/Uploader.php b/app/Libs/Uploader.php new file mode 100644 index 0000000..68f9eab --- /dev/null +++ b/app/Libs/Uploader.php @@ -0,0 +1,49 @@ +put($path.$name, $file); + + return $name; + } + + /** + * @param $file + * + * @return mixed + * + * @throws Exception + */ + public static function sanitizeFile($file): array + { + if (is_base64($file)) { + $file_data = base64_decode($file); + $f = finfo_open(); + $mime = finfo_buffer($f, $file_data, FILEINFO_MIME_TYPE); + $name = Str::random(5).time().'.'.Str::after($mime, '/'); + + return [$name, base64_decode($file)]; + } + + $name = Str::random(5).time().'.'.$file->getClientOriginalExtension(); + + return [$name, file_get_contents($file)]; + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 4139851..56f50a2 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -22,4 +22,13 @@ public function post(): BelongsTo { return $this->belongsTo(Post::class); } + + + /** + * @return BelongsTo + */ + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } } diff --git a/app/Models/Post.php b/app/Models/Post.php index 0bab158..da06a9d 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -2,6 +2,8 @@ namespace App\Models; +use Carbon\Carbon; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -25,4 +27,24 @@ public function comments(): HasMany { return $this->hasMany(Comment::class); } + + /** + * Created at format + * + * @return Attribute + */ + protected function createdAt(): Attribute + { + return Attribute::make( + get: fn ($value) => Carbon::parse($value)->diffForHumans(), + ); + } + + protected function image(): Attribute + { + // Temporary to display images generated by faker + return Attribute::make( + get: fn ($value) => str_contains($value, 'https') ? $value : url(asset('images/'.$value)), + ); + } } diff --git a/app/Policies/PostPolicy.php b/app/Policies/PostPolicy.php deleted file mode 100644 index c112def..0000000 --- a/app/Policies/PostPolicy.php +++ /dev/null @@ -1,94 +0,0 @@ -configureRateLimiting(); @@ -43,7 +43,7 @@ public function boot() * * @return void */ - protected function configureRateLimiting() + protected function configureRateLimiting(): void { RateLimiter::for('api', function (Request $request) { return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); diff --git a/app/helpers.php b/app/helpers.php new file mode 100644 index 0000000..9b2590f --- /dev/null +++ b/app/helpers.php @@ -0,0 +1,13 @@ + [ public_path('storage') => storage_path('app/public'), + public_path('images') => storage_path('app/public/images'), ], ]; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 30c87c1..d073797 100755 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -2,9 +2,8 @@ namespace Database\Seeders; -use App\Models\Comment; use App\Models\Post; -use App\Models\User; +use App\Models\Comment; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder @@ -16,6 +15,6 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - Post::factory()->has(Comment::factory()->count(3))->create(); + Post::factory()->has(Comment::factory()->count(5))->count(30)->create(); } } diff --git a/public/images b/public/images new file mode 120000 index 0000000..90efe9b --- /dev/null +++ b/public/images @@ -0,0 +1 @@ +/var/www/html/BlogPost/storage/app/public/images \ No newline at end of file diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 1f34466..a4eefaf 100755 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -1,23 +1,23 @@ @extends('layouts.app') @section('content') -
-
-
-
-
{{ __('Dashboard') }}
- -
- @if (session('status')) - - @endif +
+ - {{ __('You are logged in!') }} -
+ +
-
+ @endsection diff --git a/resources/views/includes/_createCommentForm.blade.php b/resources/views/includes/_createCommentForm.blade.php new file mode 100644 index 0000000..8dba90c --- /dev/null +++ b/resources/views/includes/_createCommentForm.blade.php @@ -0,0 +1,32 @@ +
id) }}" > + @csrf +
+ + + @if($errors->has('body')) +
+ {{ $errors->first('body') }} +
+ @endif + + @if($errors->has('post_id')) +
+ {{ $errors->first('post_id') }} +
+ @endif + +
+ + @if(session()->has('saved')) +
+ {{ session()->get('saved') }} +
+ @endif + +
+ +
+
diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index c4a59a7..2f0a86c 100755 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -49,12 +49,18 @@ @endif @else +