Linux日志
PHP记事本
JS记事本
数据库
团队资讯

迅速创建增删改模块之:控制器层

分类:PHP记事本 发布时间:2017-09-29 14:05:39 阅读:作者:郑祥景

<?php

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\Controller;
use App\Services\Manage\CategoryService;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    protected $category;
    protected $request;

    public function __construct(CategoryService $category, Request $request)
    {
        $this->category = $category;
        $this->request = $request;
    }

    /**
     * 记录列表
     *
     * @param null $keyword
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function listView($keyword = null)
    {
        $num = config('site.list_num');

        $categories = $this->category->get($num, $keyword);

        return view('manage.category.list', [
            'lists' => $categories,
        ]);
    }

    /**
     * 添加视图
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function addView()
    {
        $categories = $this->category->getParent();

        return view('manage.category.add_or_update', [
            'lists' => $categories,
            'old_input' => $this->request->session()->get('_old_input'),
            'url' => Route('category_add'),
            'sign' => 'add',
        ]);
    }

    /**
     * 修改管理员视图
     *
     * @param $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Symfony\Component\HttpFoundation\Response
     */
    public function updateView($id)
    {
        $categories = $this->category->getParent();

        try {
            $old_input = $this->request->session()->has('_old_input') ?
                session('_old_input') : $this->category->first($id);
        } catch (\Exception $e) {
            return response($e->getMessage(), $e->getCode());
        }

        return view('manage.category.add_or_update', [
            'lists' => $categories,
            'old_input' => $old_input,
            'url' => Route('category_update', ['id' => $id]),
            'sign' => 'update',
        ]);
    }

    /**
     * 添加/更新提交
     *
     * @param null $id
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function post($id = null)
    {
        $this->validate($this->request, [
            'name' => 'required',
            'parent_id' => 'required|integer',
        ]);

        try {
            $this->category->updateOrCreate($this->request->all(), $id);
        } catch (\Exception $e) {
            return response($e->getMessage());
        }

        return redirect()->route('category_list');
    }

    /**
     * 删除记录
     *
     * @param $id
     * @return bool|null
     */
    public function destroy($id)
    {
        try {
            $this->category->destroy($id);
        } catch (\Exception $e) {
            return response($e->getMessage(), 500);
        }

        return redirect()->route('category_list');
    }
}


  

   编辑:郑祥景

[随享社区版权所有 未经许可不得转载 ]

返回首页


推荐
Linux日志
PHP记事本
JS记事本
数据库
团队资讯
在线客服随享宝宝
有任何问题,都可以戳我反馈哦!
微信公众号 方便 快速
扫描二维码 关注公众号

版权所有:天妖云/随享社区V5.2 beta Copyright 2015-2019 TIAYO.COM Inc. All rights reserved.