利用laravel框架实现基本的CMS

工程的建立

1.利用laragon快速建立工程
2.XDebug调试工具的环境安装
1、在chrome浏览器安装xdebug插件
插件安装:
crx的下载地址:https://www.crx4chrome.com/crx/1716/
解压提取文件中的xdebug文件——打开chrom——更多工具——扩展程序——打开开发者模式——加载解压后的插件——加载成功——选择Debug,进入调试模式
在这里插入图片描述
在这里插入图片描述
2、在laragon中安装和配置xdebug扩展
启动laragon,在浏览器中访问http://localhost/?q=info
将上述页面的内容所有拷贝下来,粘贴到https://xdebug.org/wizard的框中,点击analyse my phpinfo() output。
在这里插入图片描述
生成本身的安装步骤安装xdebug扩展(这个安装步骤是根据本身电脑环境生成的,每一个人人可能不同)
在这里插入图片描述
到laragon中菜单——PHP——扩展查看
在这里插入图片描述
修改配置文件php.ini,保存修改,重启laragon,试一试调试结果。在这里插入图片描述
在这里插入图片描述
工程中试一试调试结果
1.在web.php中点击此处打上一个断点
在这里插入图片描述
2.在工具栏上点击此按钮进入调试监听模式
在这里插入图片描述
3.在浏览器中访问http://cms.test/看看断点是否能停下来
在这里插入图片描述php

用户认证

1.在laragon终端下执行
导入依赖包:comper require laravel/ui
在这里插入图片描述
安装部署认证组件:php artisan ui vue --auth
安装部署npm组件:nmp install && nmp run dev
命令执行后,会自动建立相关视图以及相关控制器;在web.php中自动添加相关路由
在这里插入图片描述
在这里插入图片描述
2.Auth::routes()实际注册的路由
在这里插入图片描述
3.创建数据库
到.env中配置好数据库;修改配置项
在这里插入图片描述
到终端执行数据迁徙
在这里插入图片描述
4.在浏览器中点击注册
在这里插入图片描述vue

创建控制器和设置路由

1.控制器的创建
1.php artisan make:controller CategoriesController --resource
在这里插入图片描述
class CategoriesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//
$categories = Category::query()->orderByDesc(‘created_at’)->get();laravel

return view('categories.index', compact('categories'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    //
    return view('categories.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param StoreCategory $request
 * @return Response
 */
public function store(StoreCategory $request)
{
    //
    $validatedData = $request->validated();
    Category::create($validatedData);
    $request->session()->flash('success','添加成功');
    return redirect(route('categories.index'));
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param Category $category
 * @return Response
 */
public function edit(Category $category)
{
    //
    return view('categories.create',compact('category'));
}

/**
 * Update the specified resource in storage.
 *
 * @param updateCategory $request
 * @param Category $category
 * @return void
 */
public function update(updateCategory $request, Category $category)

{
    //
        $category->update($request->validated());
        $request->session()->flash('success','更新成功');
         return redirect(route('categories.index'));
}

/**
 * Remove the specified resource from storage.
 *
 * @param Category $category
 * @return void
 * @throws \Exception
 */
public function destroy(Category $category)
{
    //
    $category->delete();
    session()->flash('success','删除成功');
    return redirect(route('categories.index'));
}

}web

2.php artisan make:controller PostsController --resource
class postsController extends Controller

{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$posts = post::query()->orderByDesc(‘created_at’)->get();
return view(‘posts.index’,compact(‘posts’));
}chrome

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    $categories = Category::all();
    return view('posts.create',compact('categories'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return false|string
 */
public function store(Request $request)
{
    $image =$request->file('image')->store('posts');
    post::create([
        'title'=>$request->title,
        'description'=>$request->description,
        'content'=>$request->content,
        'image' =>$image
    ]);

    session()->flash('success','Post created successfully.');
    return redirect(route('posts.index'));

}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{

    $post = post::withTrashed()->where('id',$id)->firstOrFail();
    if ($post->trashed()){
        $post->forceDelete();
    }else{
        $post->delete();
    }

    session()->flash('success','Post deleted successfully.');
    return redirect(route('posts.index'));

}
/**
 * Display a list of att trashed posts
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function trashed()
{
    $trashed = post::withTrashed()->get();
    return view('posts.index')->with('posts',$trashed);
}

}数据库

3.php artisan make:controller CreatePostsRequest

2.建立模型和数据库迁徙文件(注意前后顺序,先Category后Post)
Post是CMS中后台发布文章帖子
Category是文章的分类
在这里插入图片描述
php artisan make:model Category -m
在这里插入图片描述
php artisan make:model Post -m
在这里插入图片描述
3.建立表单请求StoreCategory
php artisan make:request
Category/StoreCategory
在这里插入图片描述
在这里插入图片描述
public function rules()
{
return [
//
‘name’ => ‘required|min:3|max:20|unique:categories’
];
}
public function messages()
{
return [
‘required’ => ‘:attribute 必填’,
‘name.min’ => ‘名称不能过短’,
‘name.max’ => ‘名称长度不能超过:max字符’,
‘unique’ => ‘名称:input已经存在’,
];
}
4.设置路由
在这里插入图片描述npm

视图布局

在views中建立categories文件夹和posts文件夹
各在两个文件夹中建立create.blade.php和index.blade.php
在这里插入图片描述
categories文件夹
1.create.blade.php
在这里插入图片描述
2.index.blade.php
在这里插入图片描述
posts文件夹
1.create.blade.php
在这里插入图片描述
2.index.blade.php
在这里插入图片描述浏览器

效果展现

在这里插入图片描述
这是我本身学习并利用laravel框架实现基本的CMS,写的这个博客有所不足和错误但愿您谅解并指出来,也但愿这个对您的学习laravel有用!session