资料修改优化,添加帖子分类

This commit is contained in:
fthvgb1 2017-12-31 12:50:35 +08:00
parent f249dd9f31
commit bd4990974a
11 changed files with 185 additions and 6 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Http\Requests\UserUpdate; use App\Http\Requests\UserUpdate;
use App\Models\User; use App\Models\User;
use Intervention\Image\Facades\Image;
class UsersController extends Controller class UsersController extends Controller
{ {
@ -14,14 +15,29 @@ class UsersController extends Controller
public function edit(User $user) public function edit(User $user)
{ {
$this->authorize('update', $user);
return view('users.edit', compact('user')); return view('users.edit', compact('user'));
} }
public function update(User $user, UserUpdate $request) public function update(User $user, UserUpdate $request)
{ {
$this->authorize('update', $user);
$attribute = $request->only(['name', 'email', 'introduction']); $attribute = $request->only(['name', 'email', 'introduction']);
if ($request->file('avatar')) { if ($image = $request->file('avatar')) {
$user->avatar = $request->file('avatar')->store('public/avatar'); $user->avatar = $image->store('public/avatar');
$file = Image::make(storage_path('app/' . $user->avatar));
$file->resize(1280, null, function ($constraint) {
// 设定宽度是 $max_width高度等比例双方缩放
$constraint->aspectRatio();
// 防止裁图时图片尺寸变大
$constraint->upsize();
});
// 对图片修改后进行保存
$file->save();
} }
$user->update($attribute); $user->update($attribute);
return redirect()->route('users.show', [$user])->with('success', '更新成功!'); return redirect()->route('users.show', [$user])->with('success', '更新成功!');

View File

@ -31,7 +31,7 @@ class UserUpdate extends FormRequest
], ],
'email' => ['required', 'unique:users,email,' . Auth::id()], 'email' => ['required', 'unique:users,email,' . Auth::id()],
'introduction' => 'nullable|max:255', 'introduction' => 'nullable|max:255',
'avatar' => 'image|nullable', 'avatar' => 'image|nullable|dimensions:min_width=200,min_height=200',
]; ];
} }
@ -40,6 +40,7 @@ class UserUpdate extends FormRequest
{ {
return [ return [
'introduction' => '简介', 'introduction' => '简介',
'avatar' => '头像'
]; ];
} }
} }

17
app/Models/BaseModel.php Normal file
View File

@ -0,0 +1,17 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2017/12/31
* Time: 12:44
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
}

8
app/Models/Category.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace App\Models;
class Category extends BaseModel
{
protected $guarded = ['id'];
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Policies;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class UserPolicy
{
use HandlesAuthorization;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
//
}
public function update(User $currentUser, User $user)
{
return $currentUser->id === $user->id;
}
}

View File

@ -2,6 +2,8 @@
namespace App\Providers; namespace App\Providers;
use App\Models\User;
use App\Policies\UserPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider class AuthServiceProvider extends ServiceProvider
@ -13,6 +15,7 @@ class AuthServiceProvider extends ServiceProvider
*/ */
protected $policies = [ protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy', 'App\Model' => 'App\Policies\ModelPolicy',
User::class => UserPolicy::class,
]; ];
/** /**

View File

@ -10,6 +10,7 @@ namespace App\Tools;
use Illuminate\Http\File; use Illuminate\Http\File;
use Intervention\Image\Facades\Image;
class ImageUploadTool class ImageUploadTool
{ {
@ -21,9 +22,10 @@ class ImageUploadTool
* @param $file File * @param $file File
* @param $folder * @param $folder
* @param $file_prefix * @param $file_prefix
* @param bool $max_width
* @return array|bool * @return array|bool
*/ */
public function save($file, $folder, $file_prefix) public function save($file, $folder, $file_prefix, $max_width = false)
{ {
// 构建存储的文件夹规则值如uploads/images/avatars/201709/21/ // 构建存储的文件夹规则值如uploads/images/avatars/201709/21/
// 文件夹切割能让查找效率更高。 // 文件夹切割能让查找效率更高。
@ -48,8 +50,34 @@ class ImageUploadTool
// 将图片移动到我们的目标存储路径中 // 将图片移动到我们的目标存储路径中
$file->move($upload_path, $filename); $file->move($upload_path, $filename);
// 如果限制了图片宽度,就进行裁剪
if ($max_width && $extension != 'gif') {
// 此类中封装的函数,用于裁剪图片
$this->reduceSize($upload_path . '/' . $filename, $max_width);
}
return [ return [
'path' => config('app.url') . "/$folder_name/$filename" 'path' => config('app.url') . "/$folder_name/$filename"
]; ];
} }
public function reduceSize($file_path, $max_width)
{
// 先实例化,传参是文件的磁盘物理路径
$image = Image::make($file_path);
// 进行大小调整的操作
$image->resize($max_width, null, function ($constraint) {
// 设定宽度是 $max_width高度等比例双方缩放
$constraint->aspectRatio();
// 防止裁图时图片尺寸变大
$constraint->upsize();
});
// 对图片修改后进行保存
$image->save();
}
} }

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->index()->comment('名称');
$table->text('description')->nullable()->comment('描述');
$table->integer('post_count')->default(0)->comment('帖子数');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}

View File

@ -0,0 +1,44 @@
<?php
use Illuminate\Database\Migrations\Migration;
class SeedCategoriesData extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$categories = [
[
'name' => '分享',
'description' => '分享创造,分享发现',
],
[
'name' => '教程',
'description' => '开发技巧、推荐扩展包等',
],
[
'name' => '问答',
'description' => '请保持友善,互帮互助',
],
[
'name' => '公告',
'description' => '站点公告',
],
];
\Illuminate\Support\Facades\DB::table('categories')->insert($categories);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
\Illuminate\Support\Facades\DB::table('categories')->truncate();
}
}

View File

@ -34,7 +34,7 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="user-avatar pull-left" style="margin-right:8px; margin-top:-5px;"> <span class="user-avatar pull-left" style="margin-right:8px; margin-top:-5px;">
<img src="{{Auth::user()->header}}" <img src="{{Auth::user()->header}}"
class="img-responsive img-circle" width="30px" height="30px"> class="img-responsive img-circle" style=" max-height:30px">
</span> </span>
{{ Auth::user()->name }} <span class="caret"></span> {{ Auth::user()->name }} <span class="caret"></span>
</a> </a>

View File

@ -15,7 +15,8 @@ use Illuminate\Support\Facades\Route;
Route::get('/', 'PagesController@root')->name('home'); Route::get('/', 'PagesController@root')->name('home');
Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']]); Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']])
->middleware('auth');
// Authentication Routes... // Authentication Routes...
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login'); Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');