简体   繁体   English

使用 laravel 包时未定义路由

[英]Route is not defined when using laravel package

i have created laravel package,and have one view test.blade.php showing basic form ,but inside that form when ,try to use route('contact') ,it shows me an error message我已经创建了 laravel 包,并且有一个视图 test.blade.php 显示了基本表单,但是在该表单中,当尝试使用 route('contact') 时,它向我显示了一条错误消息

Route [contact] not defined.路线 [联系人] 未定义。

my route file web.php in package folder我在包文件夹中的路由文件 web.php

<?php
// matrixhive\testpackage\src\routes\web.php
Route::get('contact', function(){
    return view('testpackage::test');
});

Route::post('contact', function(){
    echo "thanks for submission of form";
});

?>

my view file in side package我在侧包中的视图文件

<!DOCTYPE html>
<html lang="en">
<head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
      <title>Contact Us</title>
</head>
    <body>

      <div style="width: 500px; margin: 0 auto; margin-top: 90px;">
        @if(session('status'))
            <div class="alert alert-success">
                {{ session('status') }}
            </div>
        @endif

      <h3>Contact Us</h3>

      <form action="{{route('contact')}}" method="POST">
          @csrf
          <div class="form-group">
            <label for="exampleFormControlInput1">Your name</label>
            <input type="text" class="form-control" name="name" id="exampleFormControlInput" placeholder="John Doe">
          </div>
          <div class="form-group">
            <label for="exampleFormControlInput1">Email address</label>
            <input type="email" class="form-control" name="email" id="exampleFormControlInput1" placeholder="name@example.com">
          </div>

          <div class="form-group">
            <label for="exampleFormControlTextarea1">Enter Your Message</label>
            <textarea class="form-control"name="message" id="exampleFormControlTextarea1" rows="3"></textarea>
          </div>

          <button type="submit" class="btn btn-primary">Submit</button>
     </form>
    </div>
</body>
</html>

i am learning laravel package development from here我正在从这里学习 Laravel 包开发

Try this尝试这个

pass name to call route directly通过名称直接调用路由

<?php

Route::get('contact', function(){
    return view('testpackage::test');
})->name('contact');

Route::post('contact', function(){ 
    echo "thanks for submission of form";
})->name('contact');

?>

您需要使用name()方法为路由设置名称,例如

Route::post('contact', function(){ echo "thanks for submission of form"; })->name('contact');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM