Skip to content

Instantly share code, notes, and snippets.

@tuki0918
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save tuki0918/7af79f5d5733723dc778 to your computer and use it in GitHub Desktop.

Select an option

Save tuki0918/7af79f5d5733723dc778 to your computer and use it in GitHub Desktop.
laravel snippets
<?php
// app/routes.php
Route::get('/', function()
{
$artist = new Artist;
$artist->name = 'Eve 6';
$artist->save();
$album = new Album;
$album->name = 'Horrorscope';
$album->artist()->associate($artist);
$album->save();
return View::make('hello');
});
=============================================
Route::get('/', function()
{
$artist = new Artist;
$artist->name = 'Eve 6';
$artist->save();
$album = new Album;
$album->name = 'Horrorscope';
$album->artist_id = $artist->id;
$album->save();
return View::make('hello');
});
<!-- app/routes.php -->
Route::get('/', function()
{
return View::make('form');
});
Route::post('/registration', function()
{
// 全リクエストデータを取得
$data = Input::all();
// バリデーション制約を組み立てる
$rules = array(
'username' => 'alpha_num'
);
// 新しいバリデーションインスタンスを作成する
$validator = Validator::make($data, $rules);
// if ($validator->passes()) {
// // 通常はデータをここで処理する
// return 'Data was saved.';
// }
// return Redirect::to('/');
if ($validator->fails()) {
return Redirect::to('/');
}
// 通常はデータをここで処理する
return 'Data was saved.';
});
========================================================
<!-- app/views/form.blade.php -->
{{ Form::open(array('url' => 'registration')) }}
{{-- ユーザ名フィールド------------------------}}
{{ Form::label('username', 'Username') }}
{{ Form::text('username') }}
{{-- メールアドレスフィールド-------------------}}
{{ Form::label('email', 'Email address') }}
{{ Form::email('email') }}
{{-- パスワードフィールド------------------------}}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{-- パスワード確認フィールド-----------}}
{{ Form::label('password_confirmation', 'Password confirmation') }}
{{ Form::password('password_confirmation') }}
{{-- フォーム送信ボタン--------------------}}
{{ Form::submit('Register') }}
{{ Form::close() }}
<?php
<!-- app/routes.php -->
Route::get('/', function()
{
return View::make('form');
});
Route::post('/registration', function()
{
// 全リクエストデータを取得
$data = Input::all();
// バリデーション制約を組み立てる
$rules = array(
'username' => 'required|alpha_num|min:3|max:32',
'email' => 'required|email',
'password' => 'required|confirm|min:3'
);
// 新しいバリデーションインスタンスを作成する
$validator = Validator::make($data, $rules);
if ($validator->passes()) {
// 通常はデータをここで処理する
return 'Data was saved.';
}
return Redirect::to('/')->withErrors($validator);
});
========================================================
<!-- app/views/form.blade.php -->
{{ Form::open(array('url' => 'registration')) }}
<ul class="errors">
@foreach($errors->all() as $message)
<li>{{ $message }}</li>
@endforeach
</ul>
{{-- ユーザ名フィールド------------------------}}
{{ Form::label('username', 'Username') }}
{{ Form::text('username') }}
{{-- メールアドレスフィールド-------------------}}
{{ Form::label('email', 'Email address') }}
{{ Form::email('email') }}
{{-- パスワードフィールド------------------------}}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{-- パスワード確認フィールド-----------}}
{{ Form::label('password_confirmation', 'Password confirmation') }}
{{ Form::password('password_confirmation') }}
{{-- フォーム送信ボタン--------------------}}
{{ Form::submit('Register') }}
{{ Form::close() }}
========================================================
<!-- app/views/form.blade.php -->
{{ Form::open(array('url' => 'registration')) }}
{{-- ユーザ名フィールド------------------------}}
{{ $errors->first('username', '<span class="error">:message</span>') }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username') }}
{{-- メールアドレスフィールド-------------------}}
{{ $errors->first('email', '<span class="error">:message</span>') }}
{{ Form::label('email', 'Email address') }}
{{ Form::email('email') }}
{{-- パスワードフィールド------------------------}}
{{ $errors->first('password', '<span class="error">:message</span>') }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{-- パスワード確認フィールド-----------}}
{{ Form::label('password_confirmation', 'Password confirmation') }}
{{ Form::password('password_confirmation') }}
{{-- フォーム送信ボタン--------------------}}
{{ Form::submit('Register') }}
{{ Form::close() }}
<?php
// app/routes.php
// ルートのパラメーターと結びつける
Route::model('game', 'Game');
// ページ表示
Route::get('/', 'GamesController@index');
Route::get('/create', 'GamesController@create');
Route::get('/edit/{game}', 'GamesController@edit');
Route::get('/delete/{game}', 'GamesController@delete');
// 送信フォームの処理
Route::post('/create', 'GamesController@handleCreate');
Route::post('/edit', 'GamesController@handleEdit');
Route::post('/delete', 'GamesController@handleDelete');
=================================================
// app/controllers/GamesController.php
class GamesController extends BaseController
{
public function index()
{
// ゲームを一覧表示する
return View::make('index');
}
public function create()
{
// ゲームのフォームを作成する
return View::make('create');
}
public function handleCreate()
{
// 送信された作成フォームを処理する
}
public function edit(Game $game)
{
// 編集フォームを表示する
return View::make('edit');
}
public function handleEdit()
{
// 送信された編集フォームを処理する
}
public function delete()
{
// 削除の確認ページを表示する
return View::make('delete');
}
public function handleDelete()
{
// 削除の確認を処理する
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment