【问题标题】:Laravel route doesn't work. localhost8000/{route}/create always be ignoredLaravel 路线不起作用。 localhost8000/{route}/create 总是被忽略
【发布时间】:2020-08-18 21:08:54
【问题描述】:

Laravel 路由不起作用。 localhost8000/{route}/create 总是被忽略。 控制器的 create() 函数似乎没有被调用。 浏览器尝试链接到同一页面或 /home 我应该如何解决和检查这个问题?

web.php

Route::resource('/debug', 'WorkerController')->except(['index'])->middleware('auth');
class WorkController extends Controller
{
    public function create(WorkRequest $request)
    {
        Log::debug("This function never be called", "!!");

        return view("a");
    }

    public function store(WorkRequest $request)
    {
        $schedules = DB::table('schedules')->get();

        $request->date;
        $request->hours;
        $worker_id = strval($request->user()->id);
        Log::debug('date=' . $request->date);
        //omit

        $registerred_schedules = DB::table('worker_schedules')->where('worker_id', $worker_id)->get();

        return view('workers.create', ['schedules' => $registerred_schedules]);
    }
}
|        | POST      | debug                         | debug.store      | App\Http\Controllers\WorkController@store                    | web,auth   |
|        | GET|HEAD  | debug/create                  | debug.create     | App\Http\Controllers\WorkController@create                   | web,auth   |
|        | DELETE    | debug/{debug}                 | debug.destroy    | App\Http\Controllers\WorkController@destroy                  | web,auth   |
|        | PUT|PATCH | debug/{debug}                 | debug.update     | App\Http\Controllers\WorkController@update                   | web,auth   |
|        | GET|HEAD  | debug/{debug}                 | debug.show       | App\Http\Controllers\WorkController@show                     | web,auth   |
|        | GET|HEAD  | debug/{debug}/edit            | debug.edit       | App\Http\Controllers\WorkController@edit                     | web,auth   |

【问题讨论】:

  • 尝试将您的动作控制器命名为“存储”而不是“创建”
  • 你试过这条路线localhost:8000/debug/{route}/create吗?
  • 请您显示您用于访问该页面的链接的代码。
  • 当我尝试链接 /debug 然后 Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 此路由不支持 GET 方法。支持的方法:POST。

标签: laravel laravel-routing laravel-7


【解决方案1】:

在你的情况下:

路由 /debug 指向 WorkController@store 并遵循 POST 方法。 所以当你通过 GET 方法访问它时,你会得到一个异常。

而 debug/create 指向 WorkController@create 并遵循 GET 方法。

所以/debug/create 应该加载您的表单或视图,/debug 应该存储表单数据。

您的控制器中缺少 index 方法,否则它会在 /debug 上默认调用并具有 GET 方法。

【讨论】:

  • '我知道。我尝试通过 POST/GET 方法进行链接,但 WorkController 中的任何函数都不称为 ''
    localhost:8000/debug"></form>'''
  • 请在问题中添加您的表单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2013-05-03
  • 2014-09-08
相关资源
最近更新 更多