【问题标题】:How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template?如果我不使用刀片或任何模板,如何使用 href 标签重定向到 laravel 5 中的路由?
【发布时间】:2016-03-09 00:03:17
【问题描述】:
Route::get('/page','UserController@view page'); 

是我的路线。

我有一个带有 href 标记的列表,我想重定向到这条路线。

<ul>
    <li><a href="">how it works</a></li>
</ul>

我没有使用刀片或任何其他模板。

【问题讨论】:

  • 只需在href 属性中添加路径。 &lt;li&gt;&lt;a href="/page"&gt;how it works&lt;/a&gt;&lt;/li&gt;
  • 它重定向到localhost/page
  • 预期的 URL 是什么。
  • 如果不使用 laravel URL 函数,指向该 URL 会很棘手,而且不切实际,因为当您转移到生产环境时,您需要指向来自公共目录的请求。因此,对于开发,您可以在 config 中配置应用 URL 后使用虚拟主机或 laravel URL 功能
  • localhost/example/public/page

标签: redirect routes laravel-5 href


【解决方案1】:

请考虑使用 laravel 刀片从列表中编辑项目的解决方案

            <a href="{{ url('players' , [ 'id' => $player->id ]) }}/edit"
               class="btn btn-primary" role="button">Varia</a>
        </div>
    </div>
    @endforeach

【讨论】:

    【解决方案2】:

    除了@chanafdo 回答,还可以使用路由名称

    使用 laravel 刀片时

    <a href="{{route('login')}}">login here</a> 路由名称中带有参数

    当转到 URI 之类的 url 时:profile/{id} <a href="{{route('profile', ['id' => 1])}}">login here</a>

    不带刀片

    <a href="<?php echo route('login')?>">login here</a>

    在路由名称中带有参数

    当转到 URI 之类的 url 时:profile/{id} <a href="<?php echo route('profile', ['id' => 1])?>">login here</a>

    从 laravel 5.2 开始,您可以 use @php @endphp 在 laravel Blade 中创建为 &lt;?php ?&gt;。 使用刀片您的个人意见,但我建议使用它。学习它。 它有很多精彩的功能,如template inheritanceComponents & Slotssubviews 等......

    【讨论】:

    • 您需要将路线命名为Route::get('profile/{id}', 'somecontroller@somefunction')-&gt;name('profile');
    【解决方案3】:

    在您的应用配置文件中,将 url 更改为 localhost/example/public

    然后当你想链接到某个东西时

    &lt;a href="{{ url('page') }}"&gt;Some Text&lt;/a&gt;

    没有刀片

    &lt;a href="&lt;?php echo url('page') ?&gt;"&gt;Some Text&lt;/a&gt;

    【讨论】:

      猜你喜欢
      • 2016-12-18
      • 2019-08-28
      • 2017-06-20
      • 2014-06-06
      • 2016-11-29
      • 2015-07-02
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多