Skip to content

Commit aaa5525

Browse files
committed
Add user statuses
1 parent 4b988f1 commit aaa5525

22 files changed

+342
-18
lines changed

app/Http/Controllers/StaticPagesController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
use App\Http\Requests;
88
use App\Http\Controllers\Controller;
99

10+
use App\Models\Status;
11+
use Auth;
12+
1013
class StaticPagesController extends Controller
1114
{
15+
1216
public function home()
1317
{
14-
return view('static_pages/home');
18+
$feed_items = [];
19+
if (Auth::check()) {
20+
$feed_items = Auth::user()->feed()->paginate(30);
21+
}
22+
23+
return view('static_pages/home', compact('feed_items'));
1524
}
1625

1726
public function help()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
use App\Http\Controllers\Controller;
9+
10+
use App\Models\Status;
11+
use Auth;
12+
13+
class StatusesController extends Controller
14+
{
15+
public function __construct()
16+
{
17+
$this->middleware('auth', [
18+
'only' => ['store', 'destroy']
19+
]);
20+
}
21+
22+
public function store(Request $request)
23+
{
24+
$this->validate($request, [
25+
'content' => 'required|max:140'
26+
]);
27+
28+
Auth::user()->statuses()->create([
29+
'content' => $request->content
30+
]);
31+
return redirect()->back();
32+
}
33+
34+
public function destroy($id)
35+
{
36+
$status = Status::findOrFail($id);
37+
$this->authorize('destroy', $status);
38+
$status->delete();
39+
session()->flash('success', '微博已被成功删除!');
40+
return redirect()->back();
41+
}
42+
}

app/Http/Controllers/UsersController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function create()
3939
public function show($id)
4040
{
4141
$user = User::findOrFail($id);
42-
return view('users.show', compact('user'));
42+
$statuses = $user->statuses()
43+
->orderBy('created_at', 'desc')
44+
->paginate(30);
45+
return view('users.show', compact('user', 'statuses'));
4346
}
4447

4548
public function store(Request $request)

app/Http/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
post('password/email', 'Auth\PasswordController@postEmail')->name('password.reset');
1818
get('password/reset/{token}', 'Auth\PasswordController@getReset')->name('password.edit');
1919
post('password/reset', 'Auth\PasswordController@postReset')->name('password.update');
20+
21+
resource('statuses', 'StatusesController', ['only' => ['store', 'destroy']]);

app/Models/Status.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Status extends Model
8+
{
9+
protected $fillable = ['content'];
10+
11+
public function user()
12+
{
13+
return $this->belongsTo(User::class);
14+
}
15+
}

app/Models/User.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ public function setPasswordAttribute($password)
5656
{
5757
$this->attributes['password'] = bcrypt($password);
5858
}
59+
60+
public function statuses()
61+
{
62+
return $this->hasMany(Status::class);
63+
}
64+
65+
public function feed()
66+
{
67+
return $this->statuses()
68+
->orderBy('created_at', 'desc');
69+
}
5970
}

app/Policies/StatusPolicy.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Policies;
4+
5+
use Illuminate\Auth\Access\HandlesAuthorization;
6+
use App\Models\User;
7+
use App\Models\Status;
8+
9+
class StatusPolicy
10+
{
11+
use HandlesAuthorization;
12+
13+
public function destroy(User $user, Status $status)
14+
{
15+
return $user->id === $status->user_id;
16+
}
17+
}

app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Providers;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Carbon\Carbon;
67

78
class AppServiceProvider extends ServiceProvider
89
{
@@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider
1314
*/
1415
public function boot()
1516
{
16-
//
17+
Carbon::setLocale('zh');
1718
}
1819

1920
/**

app/Providers/AuthServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
77

88
use App\Models\User;
9+
use App\Models\Status;
910
use App\Policies\UserPolicy;
11+
use App\Policies\StatusPolicy;
1012

1113
class AuthServiceProvider extends ServiceProvider
1214
{
@@ -18,6 +20,7 @@ class AuthServiceProvider extends ServiceProvider
1820
protected $policies = [
1921
'App\Model' => 'App\Policies\ModelPolicy',
2022
User::class => UserPolicy::class,
23+
Status::class => StatusPolicy::class,
2124
];
2225

2326
/**

config/mail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
|
2929
*/
3030

31-
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
31+
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
3232

3333
/*
3434
|--------------------------------------------------------------------------
@@ -54,7 +54,7 @@
5454
|
5555
*/
5656

57-
'from' => ['address' => '[email protected]', 'name' => 'Aufree'],
57+
'from' => ['address' => null, 'name' => null],
5858

5959
/*
6060
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)