@if语句使用URL参数laravel blade

时间:2014-05-19 03:32:07

标签: if-statement laravel blade

我是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条件。即:“只是测试新用户页面”

提前致谢

1 个答案:

答案 0 :(得分:0)

我将避免使用你的所有代码,因为你的代码很复杂。

我将逐步解释。

提出两个观点。一个是求职者,一个是雇主。

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

让我们来代码。

<强> routes.php文件

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

<强> UserController中

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

    }

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