doujue6196 2015-01-06 22:55
浏览 12

循环时在URL :: route()中回显动态路由名称

I have an array in which each Section holds information about its status and route name:

$sections = array(
    'Section_1' => array(
        'status' => 1,
        'route' => 'section1.route'
        ),
    'Section_2' => array(
        'status' => 0,
        'route' => 'section2.route'
        ),
    'Section_3' => array(
        'status' => 1,
        'route' => 'section3.route'
        )
    );

I'm trying to loop through it to create a HTML list.

I tried this (1):

@foreach ($sections as $dataName => $details)
    <li>
        <a href="{{ URL::route($details['route']) }}">{{ $dataName }}</a>
    </li>
@endforeach

This (2):

@foreach ($sections as $dataName => $details)
    <li>
        <?php $routeName = $details['route']; ?>
        <a href="{{ URL::route($routeName) }}">{{{ $dataName }}}</a>
    </li>
@endforeach

And this (3):

@foreach ($sections as $dataName => $details)
    <li>
        <?php $routeName = URL::route($details['route']); ?>
        <a href="<?php echo $routeName; ?>">{{{ $dataName }}}</a>
    </li>
@endforeach

All of the above throw the same error:

Route [] not defined.

InvalidArgumentException("Route [{$name}] not defined.")

All the routes in the array exist - checked them for typos and so on. I have a feeling this might be something tiny but I don't see what. Any ideas?

PS. I'm aware I can store full routes in the array, like 'route' => URL::route('section1.route') etc. But what is wrong with the pieces of code above?

EDIT: Sample route

Route::get('user/show/section1',
    array(
        'before' => 'auth',
        'uses' => 'UserController@showSection1',
        'as' => 'section1.route'
        )
    );

I'm using named routes.

  • 写回答

2条回答 默认 最新

  • douqi2804 2015-01-06 23:17
    关注

    Ok, after simple test, i create simulation code like below.

    On my routes i set this:

    Route::get('user/show/section1',
        array(
            'before' => 'auth',
            'uses'   => 'HomeController@showWelcome',
            'as'     => 'section1.route'
        )
    );
    
    Route::get('user/show/section2',
        array(
            'before' => 'auth',
            'uses'   => 'HomeController@s2',
            'as'     => 'section2.route'
        )
    );
    Route::get('user/show/section3',
        array(
            'before' => 'auth',
            'uses'   => 'HomeController@s2',
            'as'     => 'section3.route'
        )
    );
    

    And simulated method on my controller:

    public function showWelcome()
        {
            $sections = array(
                'Section_1' => array(
                    'status' => 1,
                    'route' => 'section1.route'
                ),
                'Section_2' => array(
                    'status' => 0,
                    'route' => 'section2.route'
                ),
                'Section_3' => array(
                    'status' => 1,
                    'route' => 'section3.route'
                )
            );
    
            return View::make('hello')->with('sections', $sections);
        }
    
        public function s2()
        {
    
        }
    
        public function s3()
        {
    
        }
    

    Then loop it on view using your first method

    @foreach($sections as $dataName => $details)
        <li>
            <a href="{{ URL::route($details['route']) }}">{{ $dataName }}</a>
        </li>
    @endforeach
    

    And yes, i got the result as a html link:

     <li>
        <a href="http://exam.com/public/user/show/section1">Section_1</a>
     </li>
     <li>
        <a href="http://exam.com/public/user/show/section2">Section_2</a>
     </li>
     <li>
        <a href="http://exam.com/public/user/show/section2">Section_3</a>
     </li>
    

    Page source screenshot

    So check your routes.php, check if another routes taking your controller method. Its between your routes and your controller. The array has passed to view in correct way. Nothing to do with it.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行