PHP laravel自动路由

问题描述 投票:0回答:3

我正在使用laravel 5.5,我正在尝试使用自动路由到控制器,但它无法正常工作

在web.php(此版本的路由文件)中,我有以下行

Route::resource('panel', 'panel');
Route::resource('/', 'HomeController');

在小组中,我有以下行动

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class panel extends Controller{
    public function index(){
        return \View::make('panel.index');
    }

    public function registrar(){
        return \View::make('panel.registrar');
    }

}

但它只调用index()视图,当用户访问网址时,不会调用registrar()视图

site.com/panel/registrar

以下错误在屏幕上打印

"Method [show] does not exist on [App\Http\Controllers\panel]."

我试图使用base_controller,但它也不起作用

"Class 'App\Http\Controllers\Base_Controller' not found"

有没有办法识别这些行为?

php laravel laravel-5
3个回答
3
投票

资源路由设置7个特定路由,即控制器上需要的7种特定方法,7。如果您不想要所有这7条路由,则必须以这种方式定义它。

资源路由不是隐式控制器。它不会查看控制器上的方法然后制作路由。资源路由是一种“特定”的东西。我们在Laravel中没有隐式控制器,因为没有任何意义。

Laravel 5.5 Docs - Controllers - Resource Controllers

您创建的路由指向不存在的方法,即错误。

此外,Route::resource的第一个参数是资源“名称”,而不是PATH。它在技术上不是一个URI。它是资源的名称。

Route::resource('/', ...) // not a name

3
投票

这是一个具有基本CRUD操作的资源控制器,因此为了工作,您必须定义其他方法,就像在您的情况下,您应该添加方法show(),然后在该方法中呈现您想要的视图。

资源控制器必须定义以下方法:

class TestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 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)
    {
        //
    }
}

而基地控制器显然不是qazxsw poi,而是qazxsw poi

有关更多信息,请参阅qazxsw poi


2
投票

如果您不需要所有资源方法,请将此资源更改为简单的qazxsw boi

Base_Controller

并使用Controller而不是Laravel 5.5 Resource Controllers来获得没有冲突的网址

get
© www.soinside.com 2019 - 2024. All rights reserved.