routing for articles

This commit is contained in:
Dym Sohin 2024-08-09 10:37:35 +02:00
parent a71110c333
commit f5fd828697
9 changed files with 140 additions and 20 deletions

View File

@ -2,6 +2,8 @@
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Request;
class SessionController extends Controller
@ -20,7 +22,7 @@ class SessionController extends Controller
if( !Auth::attempt($validatedAttributes) )
{
throw ValidationException::withMessages(['email' => 'wrong password']);
throw ValidationException::withMessages(['email' => 'credentials do not match']);
}
request()->session()->regenerate();

47
app/Models/Article.php Normal file
View File

@ -0,0 +1,47 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
class Article //extends Model
{
use HasFactory;
public static function all(): array {
return [
[ 'id' => 1,
'title' => 'First One',
'slug' => '1st',
'body' => 'First!!1<br>
lol',
],
[ 'id' => 2,
'title' => 'Second One',
'slug' => '2nd',
'body' => 'Second i do concour<br>
lmfao',
],
[ 'id' => 3,
'title' => 'Third One',
'slug' => '3rd',
'body' => 'Third indeed<br>
xD',
],
];
}
public static function find(string $slug): array
{
$article = Arr::first(Article::all(), fn($article) => $article['slug'] === $slug);
if(! $article ) {
abort(404);
}
return $article;
}
}

View File

@ -720,3 +720,39 @@ dfn[title]
}
.x-input .error
{ grid-area : error }
ol
{ gap: 0.5rem
; flex-direction: column
}
ol
{ counter-reset : ol_lvl_1 }
ol ol
{ counter-reset : ol_lvl_2 }
ol ol ol
{ counter-reset : ol_lvl_3 }
ol > li::before
{ content : counter( ol_lvl_1 ) '.'
; counter-increment : ol_lvl_1
}
ol > li:has(ol:first-child)::before
{ display : none }
ol > li > ol:first-child > li::before
{ background : var( --bg ) }
ol ol > li::before
{ content : counter( ol_lvl_1 ) '.' counter( ol_lvl_2 ) '.'
; counter-increment : ol_lvl_2
}
ol ol > li:has(ol:first-child)::before
{ display : none }
ol ol > li > ol:first-child > li::before
{ background : var( --bg ) }
ol ol ol > li::before
{ content : counter( ol_lvl_1 ) '.' counter( ol_lvl_2 ) '.' counter( ol_lvl_3 ) '.'
; counter-increment : ol_lvl_3
}

View File

@ -0,0 +1,21 @@
<x-layout>
<main class="article">
<h1>{{ $title ?? $slug }}</h1>
<section>
{{ $body }}
</section>
</main>
@auth
<form class="col c" method="post" name="delete_form" action="/delete/{{ $slug }}">
@csrf
<a href="/edit/{{ $slug }}">Edit</a>
<button type="submit">Delete</button>
</form>
@endauth
</x-layout>

View File

@ -1,6 +1,6 @@
<x-layout>
<form class="col c" method="post" name="register_form" id="register_form" action="/register">
<form class="col c" method="post" name="register_form" action="/register">
@csrf
<x-input :value="old('fullname')"

View File

@ -21,9 +21,7 @@
@guest
<a href="/register">Register</a>
<form class="col c" method="post" name="login_form" id="login_form" action="/login">
<form class="col c" method="post" name="login_form" action="/login">
@csrf
<x-input type="email" :value="old('email')"
title="E-Mail"
@ -32,6 +30,7 @@
title="Password"
required>password</x-input>
<button type="submit">Login</button>
<a href="/register">Register</a>
</form>
@endguest
@ -39,7 +38,7 @@
{{ $slot }}
<form class="col c" method="post" name="logout_form" id="logout_form" action="/logout">
<form class="col c" method="post" name="logout_form" action="/logout">
@csrf
<button type="submit">Logout</button>
</form>

View File

@ -1,5 +1,13 @@
<x-layout>
<main class="article">
<main class="w-30 c">
{{ $greeting }}. {{ $self }}
<ol>
@foreach($articles as $article)
<li><a href="/{{ $article['slug'] }}">{{ $article['title'] ?? $article['slug'] }}</a>
@endforeach
</ol>
<img src="/index.png" width="500" alt="list of avatars used over the years, currently planning on visual collection for this type of postings">
</main>
</x-layout>

View File

@ -1,7 +0,0 @@
<x-layout>
<main class="article">
</main>
</x-layout>

View File

@ -1,10 +1,17 @@
<?php
use App\Models\Article;
use App\Http\Controllers\RegisteredUserController;
use App\Http\Controllers\SessionController;
use Illuminate\Support\Facades\Route;
Route::view('/', 'index');
Route::get('/', function() {
return view('index', [
'greeting' => 'Hello',
'self' => 'My name is Dym Sohin, im a web-developer',
'articles' => Article::all(),
]);
});
Route::view('/about', 'about');
Route::view('/graphics', 'graphics');
@ -23,10 +30,10 @@ Route::name('admin.new')->get('/new', function () {
Route::name('admin.edit')->get('/edit', function () {
return view('admin.edit');
});
Route::name('admin.edit')->get('/edit/{slug}', function ($name = null) {
Route::name('admin.edit')->get('/edit/{slug}', function ($slug = null) {
return view('admin.edit');
})->where('slug', '[A-Za-z0-9-]+');
Route::name('admin.index')->get('/admin', function ($name = null) {
Route::name('admin.index')->get('/admin', function () {
return view('admin.index');
});
@ -40,6 +47,13 @@ Route::post('/logout', [SessionController::class, 'destroy']);
Route::name('page.show')->get('/{slug}', function () {
return view('page');
Route::get('/{slug}', function (string $slug) {
$article = Article::find($slug);
return view('article',
[ 'article' => $article
, 'title' => $article['title']
, 'slug' => $article['slug']
, 'body' => $article['body']
]);
})->where('slug', '[A-Za-z0-9-]+');