generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 14
/
web.php
160 lines (129 loc) · 4.59 KB
/
web.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
use Workbench\App\Models\Message;
use Workbench\App\Models\Opengraph\OpengraphEmbed;
use Workbench\App\Models\Post;
use Workbench\App\Models\User;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::redirect('/', '/demo');
Route::get('/demo', function () {
return view('demo');
})->name('demo.index');
Route::get('/chat', function () {
return view('chat.index', [
'messages' => Message::query()
->withRichText('content')
->ordered()
->get(),
]);
})->name('chat.index');
Route::post('/messages', function (Request $request) {
$message = Message::create($request->validate([
'content' => ['required'],
]));
return redirect()->back()->withFragment("#message_{$message->id}");
})->name('messages.store');
Route::get('/posts', function () {
return view('posts.index', [
'posts' => Post::query()
->latest()
->get(),
]);
})->name('posts.index');
Route::get('/posts/create', function () {
return view('posts.create');
})->name('posts.create');
Route::post('/posts', function (Request $request) {
$post = Post::create($request->validate([
'title' => ['required'],
'body' => ['required'],
]));
return to_route('posts.show', $post);
})->name('posts.store');
Route::get('/posts/{post}', function (Post $post) {
return view('posts.show', [
'post' => $post,
]);
})->name('posts.show');
Route::get('/posts/{post}/edit', function (Post $post) {
return view('posts.edit', [
'post' => $post,
]);
})->name('posts.edit');
Route::put('/posts/{post}', function (Request $request, Post $post) {
$post->update($request->validate([
'title' => ['required'],
'body' => ['required'],
]));
return to_route('posts.show', $post);
})->name('posts.update');
Route::post('/posts/{post}/comments', function (Request $request, Post $post) {
$comment = $post->comments()->create($request->validate([
'content' => ['required'],
]));
return back()->withFragment(sprintf('#comment_%s', $comment->id));
})->name('posts.comments.store');
Route::get('/livewire', function () {
return view('livewire.index');
})->name('livewire');
Route::get('/mentions', function (Request $request) {
return User::query()
->when($request->query('search'), fn ($query, $search) => $query->where('name', 'like', "%{$search}%"))
->get()
->map(fn (User $user) => [
'sgid' => $user->richTextSgid(),
'name' => $user->name,
'content' => $user->richTextRender(),
]);
})->name('mentions.index');
Route::post('attachments', function (Request $request) {
$request->validate([
'attachment' => ['required', 'file'],
]);
$path = $request->file('attachment')->store('trix-attachments', 'public');
return [
'image_url' => route('attachments.show', $path),
];
})->name('attachments.store');
// This route wouldn't exist in a real app
Route::get('/attachments/{path}', function (string $path) {
$disk = Storage::disk('public');
abort_unless($disk->exists($path), 404);
$stream = $disk->readStream($path);
$headers = [
'Content-Type' => $disk->mimeType($path),
'Content-Length' => $disk->size($path),
];
return response()->stream(fn () => fpassthru($stream), 200, $headers);
})->name('attachments.show')->where('path', '.*');
Route::post('/opengraph-embeds', function (Request $request) {
$request->validate([
'url' => [
'bail',
'required',
'url',
function (string $attribute, mixed $value, Closure $fail): void {
$ip = gethostbyname($host = parse_url($value)['host']);
// Prevent sniffing domains resolved to private IP ranges...
if ($ip === $host || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
$fail(__('URL is invalid.'));
}
},
],
]);
if ($opengraph = OpengraphEmbed::createFromUrl($request->url)) {
return $opengraph->toArray();
}
return response()->noContent();
});