简体   繁体   English

在控制器Laravel 5.8.22中使用URL中的参数

[英]Use a parameter from URL in a controller Laravel 5.8.22

I'm trying to pass anINT from this URL: myapp.build/courses/anINT (implemented in the CoursesController ) to $id in the Lesson_unitsController function below. 我正在尝试从以下URL传递anINTmyapp.build/courses/anINT (在CoursesController实现)到下面的Lesson_unitsController函数中的$id I've tried a lot of solutions, but I can't seem to get it right. 我尝试了很多解决方案,但似乎无法正确解决。

The function in the CoursesController which implements the url is: CoursesController中实现url的函数是:

    public function show($id)
{
    $course = Course::find($id);
    return view('courses.show')->with('course', $course);
}

Part of the show.blade.php file is: show.blade.php文件的一部分是:

@if(!Auth::guest())
    @if(Auth::user()->id == $course->user_id)
        <a href="/courses/{{$course->id}}/edit" class="btn btn-outline-secondary btn-light">Edit Course</a>
        <a href="/lesson_units/specificindex" class="btn btn-outline-secondary btn-light">Lesson Units</a>

        {!!Form::open(['action'=> ['CoursesController@destroy', $course->id], 'method' => 'POST', 'class' => 'float-right'])!!}
            {{Form::hidden('_method', 'DELETE')}}
            {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
        {!!Form::close()!!}
    @endif
@endif

The Lesson_unitsController functions are: Lesson_unitsController函数为:

    public function index()
{
    $lesson_units = Lesson_unit::orderBy('title','asc')->paginate(10);
    return view('lesson_units.index')->with('lesson_units', $lesson_units);
}

public function specificindex($id)
{
    $course = Course::find($id);
    return view('lesson_units.specificindex')->with('lesson_units', $course->lesson_units);
}

And the specificindex.blade.php file is: 而且specificindex.blade.php文件是:

@extends('layouts.app')
@section('content')
<div class="container">
  <div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">Dashboard</div>
            <div class="card-body">
                <a href="/lesson_units/create" class="btn btn-primary">Create lesson unit</a>
                <p>
                <h3>Your lesson_units</h3>
                @if(count($lesson_units) > 0)
                    <table class="table table-striped">
                        <tr><th>Title</th><th></th><th></th></tr>
                        @foreach($lesson_units as $lesson_unit)
                            <tr><td>{{$lesson_unit->title}}</td>
                                <td><a href="/lesson_units/{{$lesson_unit->id}}/edit" class="btn btn-outline-secondary btn-light">Edit</a></td>
                                <td>
                                    {!!Form::open(['action'=> ['Lesson_unitsController@destroy', $lesson_unit->id], 'method' => 'POST', 'class' => 'float-right'])!!}
                                    {{Form::hidden('_method', 'DELETE')}}
                                    {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
                                    {!!Form::close()!!}
                                </td>
                            </tr>
                        @endforeach
                    </table>
                @else
                    <p>You have no lesson unit.</p>
                @endif
                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif
                You are logged in!
            </div> </div> </div> </div> </div>

@endsection @endsection

The routes in web.php are: web.php中的路由为:

Route::resource('courses', 'CoursesController');
Route::resource('lesson_units', 'Lesson_unitsController');
Route::get('/courses/{id}', 'Lesson_unitsController@specificIndex');

I want that when the link for Lesson Units is clicked on the page, the id in the url is passed to the specificindex function in the Lesson_unitsController . 我希望当在页面上单击“ Lesson Units的链接时,URL中的id传递给Lesson_unitsControllerspecificindex函数。 Now, I get just a blank page. 现在,我只有空白页。 What am I doing wrong? 我究竟做错了什么?

You don't have a route set up to handle the $id coming in. The resource method within the Route class will provide a GET route into your Lesson_unitsController controller without an expectation of any variable. 您没有设置用于处理传入$id路由 Lesson_unitsController类中的resource方法将为您的Lesson_unitsController控制器提供GET路由,而无需任何变量。 It is the default index route, and by default doesn't pass a variable. 这是默认的索引路由,默认情况下不传递变量。

There are a couple of ways to do this, but the easiest is to just create a new route for your specific need: 有两种方法可以做到这一点,但是最简单的方法就是根据您的特定需求创建一条新路线:

Route::get('lesson_units/{id}', 'Lesson_unitsController@specificIndex');

And then make your specificIndex function in your controller with an incoming variable: 然后使用输入变量在控制器中创建specificIndex函数:

public function specialIndex($id)
{
 $course = Course::find($id);
   // return view to whatever you like
}

HTH HTH

Try to understand the concept of RESTful and CRUD . 尝试了解RESTfulCRUD的概念。

By using Route::resource('courses', 'CoursesController'); 通过使用Route::resource('courses', 'CoursesController'); , Laravel has helped you to register the following routes: ,Laravel帮助您注册了以下路线:

Route::get('courses', 'CoursesController@index');
Route::get('courses/create', 'CoursesController@create');
Route::post('courses/{course}', 'CoursesController@store');
Route::get('courses/{course}/edit', 'CoursesController@edit');
Route::put('courses/{course}', 'CoursesController@update');
Route::delete('courses/{course}', 'CoursesController@destroy');

Then, when you make GET request to myapp.build/courses/123 , Laravel will pass the request to the show function of your CoursesController like: 然后,当你GET请求myapp.build/courses/123 ,Laravel将请求传递到show您的功能CoursesController ,如:

public function show(Course $course)
{
 return view('lesson_units.index')->with('lesson_units', $course->lesson_units);
}

Laravel will automatically resolve the Course from your database using the parameter passed into the route myapp.build/courses/{course} . Laravel将使用传递给路由myapp.build/courses/{course}的参数从数据库自动解析 Course

Note: The variable name $course has to match with the one specify in route /{course} . 注意:变量名称$course必须与route /{course}指定的变量匹配。

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

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