【问题标题】:@if statement using URL parameter laravel blade使用 URL 参数 laravel Blade 的 @if 语句
【发布时间】:2014-07-06 21:59:30
【问题描述】:

我是 laravel 和刀片的新手。我是一名学生,正在做一个简单的“求职者”作业。我有 2 种不同类型的用户 - 求职者(第 1 类)和雇主(第 2 类)。当我从 layout.blade.php 中的按钮创建新用户时,用户将单击注册(类别 1)链接或雇主按钮(类别 2),我想将类别传递给 create.blade.php 所以我可以根据它们的类别对其进行样式化,当然还要对实际用户隐藏这些信息。

我不确定你想看什么代码,但我会从我的 layout.blade.php 开始——当点击链接或按钮时,它会重定向到 create.blade.php 并且 url 会更新为类别 1 或类别 2 - 取决于单击的内容。我想添加一个 @if 语句,说明显示哪个创建对象,一个求职者或一个雇主(他们的选项略有不同)

layout.blade.php

<div class="col-sm-9">
@if (!Auth::check())
<div class="login-form">
{{ Form::open(array('action' => 'UserController@login')); }}
{{ Form::text('username', null, array('class' => 'input-small', 'placeholder' => 'Email')); }}
{{ Form::password('password', array('class' => 'input-small', 'placeholder' => 'Password')); }}
{{ Form::submit('Sign in', array('class' => 'btn btn-danger')); }}
{{ Form::close(); }}
{{ Form::open(array('action' => 'UserController@create')); }}
{{link_to_route('user.create', 'or Register here', ['category' => 1] )}}
</div>
{{link_to_route('user.create', 'Employers', ['category' => 2], array('class' => 'btn btn-primary')) }}
@endif
@yield('content1') 

create.blade.php

@extends('job.layout')
@section('content1')
@if('category' == 2)
<h1>New Employer page</h1>
{{ Form::open(array('action' => 'UserController@store')); }}
{{ Form::text('username', null, array('class' => 'input-small', 'placeholder' => 'Email')); }}
<p>{{ Form::password('password', array('class' => 'input-small', 'placeholder' => 'Password')); }}
{{ Form::hidden('category', 2) }}
{{ Form::label('name', 'Name:', array('class' => 'col-sm-3')) }}
{{ Form::text('name') }}
{{ Form::label('description', 'Company Description:', array('class' => 'col-sm-3')) }}
{{ Form::text('description') }}
{{ Form::label('industry', 'Industry', array('class' => 'col-sm-3')) }}
{{ Form::text('industry') }}
{{ Form::label('phone', 'Phone Number:', array('class' => 'col-sm-3')) }}
{{ Form::text('phone') }}
<p>{{ Form::submit('Sign in'); }}
{{ Form::close(); }}
@else
<p>just a test for New User Page
@endif
@stop

到目前为止,创建页面只是返回了@else 条件。即:“只是对新用户页面的测试”

提前致谢

【问题讨论】:

    标签: if-statement laravel blade


    【解决方案1】:

    我会避开你所有的代码,因为你让它变得比现在复杂得多。

    我会一步一步解释。

    制作两个视图。一份给求职者,一份给雇主。

    根据类别,加载相应的视图。这就是你想要的。

    让我们来写代码吧。

    Routes.php

    Route::get('create/{category}', array(
                         'as'        =>      'create',
                         'uses'      =>      'UserController@create'
                            ));
    

    用户控制器

    public function create($category)
        {
           if($category==1)
               return View::make('seeker');
           elseif($category==2)
               return View::make('employer');
           else
               App::abort(400);
    
        }
    

    就是这样。无需触摸布局。尽可能避免将逻辑放入布局中。从长远来看,这将是一团糟。

    【讨论】:

    • 我打算制作 2 个单独的视图,因为这比尝试使用 1 更有意义。我认为我的任务只允许我制作 1 个“创建”视图。会按照你的方式做,看看我怎么走。谢谢:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多