【问题标题】:How to read new function in Laravel with InfyOm Laravel Generator?如何使用 InfyOm Laravel Generator 读取 Laravel 中的新功能?
【发布时间】:2017-04-18 04:28:06
【问题描述】:

我使用 InfyOm Laravel 生成器制作了控制器、模型、视图等。

我跑:php artisan infyom:scaffold Hotel

这个命令来自:http://labs.infyom.com/laravelgenerator/docs/5.3/getting-started#generator-commands

它生成控制器、模型、视图等

控制器酒店是这样的:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\CreatehotelRequest;
use App\Http\Requests\UpdatehotelRequest;
use App\Repositories\hotelRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class hotelController extends AppBaseController
{
    /** @var  hotelRepository */
    private $hotelRepository;

    public function __construct(hotelRepository $hotelRepo)
    {
        $this->hotelRepository = $hotelRepo;
    }

    /**
     * Display a listing of the hotel.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->hotelRepository->pushCriteria(new RequestCriteria($request));
        $hotels = $this->hotelRepository->all();

        return view('hotels.index')
            ->with('hotels', $hotels);
    }

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

    public function test()
    {
        die('test view');
    }

    /**
     * Store a newly created hotel in storage.
     *
     * @param CreatehotelRequest $request
     *
     * @return Response
     */
    public function store(CreatehotelRequest $request)
    {
        $input = $request->all();

        $hotel = $this->hotelRepository->create($input);

        Flash::success('Hotel saved successfully.');

        return redirect(route('hotels.index'));
    }

    /**
     * Display the specified hotel.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $hotel = $this->hotelRepository->findWithoutFail($id);

        if (empty($hotel)) {
            Flash::error('Hotel not found');

            return redirect(route('hotels.index'));
        }

        return view('hotels.show')->with('hotel', $hotel);
    }

    /**
     * Show the form for editing the specified hotel.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $hotel = $this->hotelRepository->findWithoutFail($id);

        if (empty($hotel)) {
            Flash::error('Hotel not found');

            return redirect(route('hotels.index'));
        }

        return view('hotels.edit')->with('hotel', $hotel);
    }

    /**
     * Update the specified hotel in storage.
     *
     * @param  int              $id
     * @param UpdatehotelRequest $request
     *
     * @return Response
     */
    public function update($id, UpdatehotelRequest $request)
    {
        $hotel = $this->hotelRepository->findWithoutFail($id);

        if (empty($hotel)) {
            Flash::error('Hotel not found');

            return redirect(route('hotels.index'));
        }

        $hotel = $this->hotelRepository->update($request->all(), $id);

        Flash::success('Hotel updated successfully.');

        return redirect(route('hotels.index'));
    }

    /**
     * Remove the specified hotel from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $hotel = $this->hotelRepository->findWithoutFail($id);

        if (empty($hotel)) {
            Flash::error('Hotel not found');

            return redirect(route('hotels.index'));
        }

        $this->hotelRepository->delete($id);

        Flash::success('Hotel deleted successfully.');

        return redirect(route('hotels.index'));
    }
}

我这样调用URL:http://localhost/adminlte-generator/public/hotels/create,结果是:酒店视图

我添加了新功能,功能是测试。然后我这样调用 URL:http://localhost/adminlte-generator/public/hotels/test。它没有显示测试视图。

有什么办法可以解决我的问题吗?

【问题讨论】:

    标签: php laravel generator laravel-5.3


    【解决方案1】:

    看看laravel路由

    https://laravel.com/docs/5.3/routing

    你必须在你的路线上注册它

    示例(这应该适用于您的 route/web.php)

    Route::get('adminlte-generator/public/hotels', ['uses' =&gt; 'hotelController@test']);

    我怀疑/create 的工作方式。也许他们在您运行 artisan 命令时为您提供了 route::resource。我不确定。 :)

    编辑

    先试试这个:

    Route::get('adminlte-generator/public/hotels', function() {
        echo 'Working.';
    });
    

    访问路线,如果你看到工作。问题出在您的控制器上。

    尝试在你的路线上添加这个:

    public function test() {
        echo 'Working in controller.';
    }
    

    这可以帮助您查找/识别错误。

    希望对你有帮助!

    【讨论】:

    • 我已经在我的路线中注册了它。但是,它不起作用
    • 对我来说答案是对的。 Infyom 提供脚手架/api 控制器并添加资源路由。如果你给它们添加任何新的功能,你必须提供路线。
    • 不要放弃,我最近也遇到了一些路线问题。稍微扩展语法: Route::get('adminlte-generator/public/hotels', ['as'=>'public-hotels', 'uses' => 'hotelController@test']) 。使用“公共酒店”获取路线。
    • @cssBlaster21895,你确定它有效吗?你试过了吗?我试过了,还是不行
    • 当没有任何工作时,我只运行 php artisan optimize 和 composer dump-autoload , ,但可以肯定的是,现在你只有路由问题
    猜你喜欢
    • 2021-09-16
    • 2019-02-20
    • 2016-11-15
    • 2017-04-28
    • 1970-01-01
    • 2019-12-29
    • 2021-12-14
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多