urlsnail/app/Http/Controllers/BookmarkController.php

116 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Models\Bookmark;
use Illuminate\Http\Request;
2024-05-30 23:01:16 +00:00
use Illuminate\Routing\Controllers\HasMiddleware;
2024-05-30 23:01:16 +00:00
class BookmarkController extends Controller implements HasMiddleware
{
2024-05-30 23:01:16 +00:00
public static function middleware()
{
return [
'auth',
];
}
/**
* Display a listing of the resource.
*/
2024-05-30 23:01:16 +00:00
public function index(Request $request)
{
2024-05-25 12:47:48 +00:00
return view(
'bookmarks.index', [
2024-05-30 23:01:16 +00:00
'bookmarks' => $request->user()->bookmarks()->orderByDesc('created_at')->paginate(20),
2024-05-25 12:47:48 +00:00
]
);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
2024-05-30 20:55:08 +00:00
return view('bookmarks.edit', ['bookmark' => null]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
2024-05-30 20:55:08 +00:00
$bookmark = new Bookmark;
2024-05-30 23:01:16 +00:00
$bookmark->user()->associate($request->user());
2024-05-30 20:55:08 +00:00
$bookmark->title = $request->post('title');
$bookmark->description = $request->post('description', '');
$bookmark->href = $request->post('href');
$bookmark->save();
$bookmark->syncTagsFromString($request->post('tags', ''));
return redirect()->action(
[self::class, "show"], [
"bookmark" => $bookmark,
]
);
}
/**
* Display the specified resource.
*/
2024-05-30 23:01:16 +00:00
public function show(Request $request, Bookmark $bookmark)
{
2024-05-30 23:01:16 +00:00
abort_unless($bookmark->user()->is($request->user()), 404);
return view(
2024-05-25 12:47:48 +00:00
'bookmarks.show', [
2024-05-26 13:41:28 +00:00
'bookmark' => $bookmark,
]
);
}
/**
* Show the form for editing the specified resource.
*/
2024-05-30 23:01:16 +00:00
public function edit(Request $request, Bookmark $bookmark)
{
2024-05-30 23:01:16 +00:00
abort_unless($bookmark->user()->is($request->user()), 404);
2024-05-26 12:48:04 +00:00
return view(
'bookmarks.edit', [
2024-05-26 13:41:28 +00:00
'bookmark' => $bookmark,
2024-05-26 12:48:04 +00:00
]
);
}
/**
* Update the specified resource in storage.
*/
2024-05-26 13:41:28 +00:00
public function update(Request $request, Bookmark $bookmark)
{
2024-05-30 23:01:16 +00:00
abort_unless($bookmark->user()->is($request->user()), 404);
2024-05-26 13:41:28 +00:00
$bookmark->title = $request->post('title');
$bookmark->description = $request->post('description', '');
$bookmark->href = $request->post('href');
$bookmark->save();
2024-05-30 20:55:08 +00:00
$bookmark->syncTagsFromString($request->post('tags', ''));
2024-05-29 21:07:23 +00:00
2024-05-26 13:41:28 +00:00
return redirect()->action(
[self::class, "show"], [
"bookmark" => $bookmark,
]
);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}