【问题标题】:How to send variable from blade to controller without changing the url如何在不更改 url 的情况下将变量从刀片发送到控制器
【发布时间】:2020-04-29 10:50:19
【问题描述】:

在刀片中,我有一个书籍清单。我想选择一本特定的书来显示它的信息。为此,我想通过路由将书的 id 与 href 一起发送到我的控制器。

例如我有

 <div class="body text-center">
 <a href="{{HERE!}}"><h6><b>{{($book->getName())}}</b></h6></a>
 </div> 

在 href 我想添加 $bookId = $book->id 和路由名称,以便我可以使用特定名称调用路由,该名称调用控制器中可以使用变量 $bookId 的方法

 Route::get('/infromation','Books\BookController@index')->name('info');

【问题讨论】:

  • 我不确定能得到你想要的。您不想更改 URL,但想添加 ID。 (也许是一个 POST 请求?)。路由是否也应该返回index
  • url: /books -> 显示所有书籍的位置我选择其中一本书,我希望显示的 url 变为:/{book-name}/info 或只是 /info,我也想要将所选书籍的 id 发送到控制器 Bookcontroller。
  • 如果你想在 URL 中的书名建议你有一个slug 你可以接受吗?
  • 我不知道。我去看看
  • 另外,我不推荐在不使用idslug 的情况下使用/info 之类的URL,因为在我看来,它会在尝试访问资源时提供糟糕的用户体验。跨度>

标签: laravel laravel-blade laravel-6 laravel-controller laravel-route


【解决方案1】:

这里有两个命题:

  • 第一个是使用spatie/laravel-sluggable在URL中包含书名
  • 第二种是在不改变URL的情况下通过POST请求访问图书

使用spatie/laravel-sluggable

slug 会在图书创建时从name 自动生成。

your-migration.php

 Schema::create('books', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('slug')->unique()->index();
    $table->string('name');
    // ...
    $table->timestamps();
});

web.php

// Change the URIs as you want. `{book}` is mandatory to retrieve the book though.
Route::get('/books','Books\BookController@index')->name('book.index');
Route::get('/books/{book}','Books\BookController@show')->name('book.show');

Book.php

use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;

class Book extends Model
{
    use HasSlug;

    protected $guarded = [];

    public function getSlugOptions()
    {
        // Adapt with what you want
        return SlugOptions::create()
            ->generateSlugsFrom('name')
            ->saveSlugsTo('slug')
            ->doNotGenerateSlugsOnUpdate();
    }

    public function getRouteKeyName()
    {
        return 'slug';
    }

}

BookController.php

class BookController extends Controller
{
    public function index()
    {
        return view('book.index');
    }

    public function show(Book $book)
    {
        // $book is retrieving using Model Binding: https://laravel.com/docs/5.8/routing#route-model-binding 
        return view('book.show', compact('book'));
    }
}

index.blade.php

<div class="body text-center">
    <a href="{{ route('book.show', $book) }}">
        <h6><b>{{ $book->getName() }}</b></h6>
    </a>
</div> 

使用 POST 请求(URI 不变)且不使用 SLUG

为了用户体验,我不建议使用它。

  • 用户无法为图书添加书签或与其他人共享链接
  • 刷新页面时,会提示用户是否要重新提交表单请求

web.php

Route::get('/books','Books\BookController@index')->name('book.index');
Route::post('/books','Books\BookController@show')->name('book.show');

BookController.php

class BookController extends Controller
{
    public function index()
    {
        return view('book.index');
    }

    public function show()
    {
        $book = Book::findOrFail(request('book_id'));
        return view('book.show', compact('book'));
    }
}

index.blade.php

<div class="body text-center">
    <form action="{{ route('book.show') }}" method="POST">
        @csrf
        <input type="hidden" value="{{ $book->id }}" name="book_id">
        <h6>
            <button type="submit"> 
                <b>{{ $book->getName() }}</b>
            </button>
        </h6>
    </form>
</div> 

您可以删除默认按钮样式,使其看起来像一个链接 https://stackoverflow.com/a/45890842/8068675

【讨论】:

    【解决方案2】:

    你可以这样试试

        <form action="/BookName/information/<?php echo $book->id; ?>" method="post">
          <div class="body text-center">
           <input type="hidden" name="book_id" value="{{ $book->id }}">
           <a href="/information/<?php echo $book->id; ?>">
              <button type="submit" name="book_information" class="btn btn-primary"> 
                <h6>
                  <b>{{($book->getName())}}</b> 
                </h6>
             </button>
         </div>
    
       </form> 
    
        // make route like this
        Route::post('/BookName/information/{id}','Books\BookController@index');
    
        // Access the that id in controller
        public function index(Request $request)
        {
            echo $request->book_id;
        }
    

    【讨论】:

    • 好的,但我不想在 url 上显示 id 是否可以发送 id 并保留 url '/information' 或发送 id 并将 url 更改为 '/bookName/信息”?
    • 我根据你说的更新我的答案,所以请检查
    猜你喜欢
    • 2021-11-24
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2014-11-22
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多