【问题标题】:How to handle two parameters in laravel when passing from a route to a page从路由传递到页面时如何处理laravel中的两个参数
【发布时间】:2015-10-29 01:40:48
【问题描述】:

我正在通过以下两条路线

Route::get('library/course/{id}', function($id){
    return View::make('course')->with('id',$id);
});

Route::get('library/course/{id}/{video}', function($id, $video){
    $array = array('id' => '$id', 'video' => '$video');
    return View::make('course')->withvideos($array);
});

两条路线都正常工作,但只是一一到达 course.blade.php 页面。如果我删除第一条路线的内容,则第二条路线有效,否则会给我一个错误。我该如何处理页面上的这两条路线?

******更新

我的课程页面的代码如下。 请注意,我正在使用此页面中的第一条路线。我无法使用第二个,因为它会出错。

@extends('layouts.master')

@section('content')



    <div class="row" style="margin-top: 5%;">
        <div class="large-4 columns">
            <?php $coursesName = DB::table('topics')->select('title')->get(); ?>
            <ul class="side-nav">
                @foreach($coursesName as $courseName)
                <li><a href="#">{{ $courseName->title }}</a></li>
                @endforeach
            </ul>
        </div>
        <div class="large-8 columns" data-equilizer>
            <?php $courseViews = DB::table('courses')->whereid($id)->get();  ?>

            @foreach($courseViews as $courseView)

                <?php $requiredId = $courseView->id; ?>

                <?php  $courseVideos = DB::table('videos')->whereid($requiredId)->get(); ?>

                @foreach($courseVideos as $courseVideo)
                    <!--<iframe src="https://player.vimeo.com/video/{{ $courseVideo->link }}?color=ff9933&title=0&byline=0&portrait=0' width='700' height='400' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen"></iframe>-->
                    <br>
                    <a href="{{ $courseView->id }}/{{ $courseVideo->id }}">{{ $courseVideo->name }}</a>
                @endforeach

            @endforeach


        </div>



    </div>
@stop

【问题讨论】:

  • 错误是什么?你能把course.blade.php文件的内容贴在这里吗?我试过你的路线,它适用于我本地机器上的两条路线。
  • 你使用的是哪个版本的 Laravel?
  • 您已经在这里stackoverflow.com/questions/31840122/… 提出了这个问题,但是在提出这个新问题时您没有更新损坏的代码。我不明白。
  • 我用代码参考更新了我的问题。看看这个。希望它可以帮助你给我解决方案。

标签: laravel laravel-4 laravel-5 laravel-routing


【解决方案1】:

如果认为你的错误在这里:$array = array('id' =&gt; '$id', 'video' =&gt; '$video');

当您编写'$id' 时,它会分配一个字符串并且不使用变量的内容。

尝试将此行更改为$array = array('id' =&gt; $id, 'video' =&gt; $video);

注意:如果你使用 PHP 5.4+,你可以使用[]而不是array()

【讨论】:

  • 我已经在这里告诉他了:stackoverflow.com/questions/31840122/… -- 不知道他为什么还在粘贴这个损坏的代码。
  • 我用代码参考更新了我的问题。看看这个。希望它可以帮助你给我解决方案。
【解决方案2】:

作为@Romain Lanz,我也想提一下,如果你这样写你的路线

Route::get('library/course/{id}', function($id){
    return View::make('course')->with('id',$id);
});

Route::get('library/course/{id}/{video}', function($id, $video){
    $array = array('id' => '$id', 'video' => '$video');
    return View::make('course')->withvideos($array);
});

它总是匹配 'library/course/{id}' 而不是 'library/course/{id}/{video}'

你应该这样写你的路线

Route::get('library/course/{id}/{video}', function($id, $video){
    $array = array('id' => $id, 'video' => $video);//Also change your array value
    return View::make('course')->withvideos($array);
});

Route::get('library/course/{id}', function($id){
    return View::make('course')->with('id',$id);
});

【讨论】:

  • 我用代码参考更新了我的问题。看看这个。希望它可以帮助你给我解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-14
  • 2016-07-01
  • 1970-01-01
相关资源
最近更新 更多