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

时间:2019-06-18 01:18:28

标签: php laravel laravel-5

我正尝试从以下anINT函数的URL:myapp.build/courses/anINT(在CoursesController中实现)传递$idLesson_unitsController。我尝试了很多解决方案,但似乎无法正确解决。

CoursesController中实现url的函数是:

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

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

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);
}

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

web.php中的路由为:

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

我希望在页面上单击Lesson Units的链接时,URL中的id被传递到specificindex中的Lesson_unitsController函数。现在,我只有空白页。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

您没有设置处理进入$id的路由。 Route 类中的资源方法将提供 GET 路由您的Lesson_unitsController控制器,不需要任何变量。这是默认的索引路由,默认情况下不传递变量。

有两种方法可以做到这一点,但是最简单的方法就是为您的特定需求创建一条新路线:

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

然后使用传入变量在控制器中创建specificIndex函数:

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

HTH

答案 1 :(得分:1)

尝试理解 RESTful CRUD 的概念。

Laravel通过使用 $name = DB::connection('conn_name')->table('employee')->select(DB::connection('conn_name')->raw("encode(decrypt(name, 'key', 'aes'), 'escape')"))->get(); 帮助您注册了以下路线:

Route::resource('courses', 'CoursesController');

然后,当您向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'); 进行 GET 请求时,Laravel会将请求传递给myapp.build/courses/123的{​​{1}}函数,例如:

show

Laravel将使用传递到路由CoursesController的参数从数据库中automatically resolve public function show(Course $course) { return view('lesson_units.index')->with('lesson_units', $course->lesson_units); }

注意:变量名Course必须与路由myapp.build/courses/{course}中指定的变量名匹配。