您的位置:首页 > 编程语言 > PHP开发

利用laravel框架实现基本的CMS

2020-07-18 05:09 633 查看

工程的创建

1.利用laragon快速创建工程
2.XDebug调试工具的环境安装
一、在chrome浏览器安装xdebug插件
插件安装:
crx的下载地址:https://www.crx4chrome.com/crx/1716/
解压提取文件中的xdebug文件——打开chrom——更多工具——扩展程序——打开开发者模式——加载解压后的插件——加载成功——选择Debug,进入调试模式


二、在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/看看断点是否能停下来

用户认证

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.在浏览器中点击注册

建立控制器和设置路由

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();

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'));
}

}

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’));
}

/**
* 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.设置路由

视图布局

在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有用!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐