【问题标题】:Laravel controller function not found未找到 Laravel 控制器功能
【发布时间】:2015-07-19 12:24:24
【问题描述】:

目前我正在使用 laravel。当我想通过视图访问控制器时。我收到找不到我的方法的错误。

Action App\Http\Controllers\RidesController@edit 未定义。 (看法: C:\wamp\www\resources\views\rides\rides.blade.php)

但是当我尝试 index 方法时,它的工作方式是否足够奇怪?我该怎么办?

这是我的rides.blade.php:

  </thead>
                            <tbody>
                                 <tbody>
                                    @foreach($rides as $ride)
                                     <tr>
                                        <td><a href="{!! action('RidesController@edit',$ride->id) !!}">{!! $ride->id !!}</a> </td>
                                        <td>{!! $ride->created_at !!}</td>
                                        <td>{!! $ride->beginstand !!}</td>
                                        <td>{!! $ride->eindstand !!}</td>
                                        <td>{!! $ride->van !!}</td>
                                        <td>{!! $ride->naar !!}</td>
                                        <td>{!! $ride->bezoekadres !!}</td>
                                        <td>{!! $ride->geredenroute !!}</td>
                                        <td>{!! $ride->karakterrit !!}</td>
                                        <td>{!! $ride->toelichting !!}</td>
                                        <td>{!! $ride->kilometerszakelijk !!}</td>
                                        <td>{!! $ride->kilometersprive !!}</td>
                                    </tr>
                                     @endforeach
                                </tbody>    

还有我的路线控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\ridesregistration;
use App\Http\Requests\RideFormRequest;

class RidesController extends Controller
{
    public function index()
    {
        $rides = ridesregistration::all();
        return view("rides.rides",compact('rides'));
    }

    public function create() 
    {
        return view('rides.create');
    }

    public function store(rideFormRequest $request)
    {
        $rides = new ridesregistration(array(
            'beginstand' =>$request->get('beginstand'),
            'eindstand' =>$request->get('eindstand'),
            'van' =>$request->get('van'),
            'naar' =>$request->get('naar'),
            'bezoekadres' =>$request->get('bezoekadres'),
            'geredenroute' =>$request->get('geredenroute'),
            'karakterrit' =>$request->get('karakterrit'),
            'toelichting' =>$request->get('toelichting'),
            'kilometerszakelijk' =>$request->get('kilometerszakelijk'),
            'kilometersprive' =>$request->get('kilometersprive')
            ));
        $rides->save();
        return redirect('/maakrit')->with('message','Rit '.$rides->id.' is succesvol aangemaakt.');
    }

    public function edit($id)
    {
        $rides = ridesregistration::whereId($id)->firstOrFail();
        return view('rides.edit',compact('rides','rides'));
    }
}

【问题讨论】:

  • 为什么你的模型目录和类名使用小写字母?约定显然是 CamelCase。

标签: php laravel


【解决方案1】:

根据 laravel 5 文档 http://laravel.com/docs/5.0/controllers

你应该使用带有 action() 助手的命名空间

http://joxi.ru/8AnBMZXSv8lpAO

如果我们深入研究 \Illuminate\Routing\UrlGenerator::action

我们会看到:

public function action($action, $parameters = [], $absolute = true)
{
    if ($this->rootNamespace && !(strpos($action, '\\') === 0)) {
        $action = $this->rootNamespace.'\\'.$action;
    } else {
        $action = trim($action, '\\');
    }

    if (!is_null($route = $this->routes->getByAction($action))) {
        return $this->toRoute($route, $parameters, $absolute);
    }

    throw new InvalidArgumentException("Action {$action} not defined.");
}

您的 action('RidesController@index') 有效,因为它是在路线中定义的。 并且您的操作('RidesController@edit',$ride->id) 不起作用,因为它不在路线中。

在你的 app/Http/routes.php 中定义这个控制器动作

【讨论】:

  • 感谢您的回复。但是我怎么可能访问 RidesController@index 而不是 RidesController@edit?
猜你喜欢
  • 2017-06-09
  • 2019-07-12
  • 2019-10-21
  • 2017-05-04
  • 1970-01-01
  • 2020-01-07
  • 1970-01-01
  • 2016-02-04
  • 2014-10-30
相关资源
最近更新 更多