【问题标题】:Route is not defined when using laravel package使用 laravel 包时没有定义路由
【发布时间】:2020-04-20 08:39:28
【问题描述】:

我已经创建了 laravel 包,并且有一个视图 test.blade.php 显示基本表单,但是在该表单中,当尝试使用 route('contact') 时,它会显示一条错误消息

路线 [联系人] 未定义。

我的路由文件 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";
});

?>

我在侧包中的视图文件

<!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>

我正在向here学习laravel包开发

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    试试这个

    通过名字直接调用路由

    <?php
    
    Route::get('contact', function(){
        return view('testpackage::test');
    })->name('contact');
    
    Route::post('contact', function(){ 
        echo "thanks for submission of form";
    })->name('contact');
    
    ?>
    

    【讨论】:

    • 快乐编码 :) @Jatin parmar
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 2021-05-03
      • 2021-09-10
      • 2018-12-07
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-07-22
      • 2014-09-16
      相关资源
      最近更新 更多