1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ACCON(Powered by FuelPHP):サンプルコード(シンプル系)

Last updated at Posted at 2015-07-25

ACCON(Powered by FuelPHP)を利用した実装例を示します

ブログ管理機能・画面遷移図で登場したブログの投稿種類を管理するSlug機能(投稿種類のマスターメンテナンス機能)の実装内容です。
機能は、投稿種類マスターの検索、一覧、詳細、追加、変更、削除、各バリデーション。
他に、アクセス制御(上の機能単位に実行権限の設定が可能)、画面遷移、パジネーション(ページング)、検索履歴、セキュリティ、操作履歴などは実装不要。(フレームワーク側が自動で動作)
コントローラーはモデルの宣言のみでOK。

画面とソースコードの関係

拡大

ソースコード

Controller

app/classes/controller/blog/slug.php

<?php

class Controller_Blog_Slug extends \Accon\Foundation\Controller_Physical_Crud
{
	const SERVICE_TITLE = 'Slug';
	const MODEL = Model\Slug::class;
}

Model

app/classes/model/slug.php

<?php

namespace Model;

class Slug extends \Accon\Foundation\Model_Crud
{
	protected static $_table_name = 'slug';

	protected static $_belongs_to = array(
		'user' => array(
			'model_to' => 'Accon\Model\User',
			'key_from' => 'user_id',
			'key_to' => 'id',
			'cascade_save' => false,
			'cascade_delete' => false,
		),
	);

	public static function validate($factory)
	{
		$val = \Validation::forge($factory);
		$val->add_field('name', 'Name', 'required|max_length[255]');
		return $val;
	}

	protected function _before_insert()
	{
		$this->user_id = ($this->user_id = \Auth::get_user_id()) ? $this->user_id[1] : 0;
	}

	protected function _before_update()
	{
		$this->_before_insert();
	}

}

View

app/views/blog/slug/_form.php

<fieldset>
	<div class="form-group <?php echo !$val->error('name') ?: 'has-error' ?>">
		<?php echo Form::label('名前', 'name', array('class' => 'control-label')); ?>
		<?php echo Form::input('name', Input::post('name', isset($model) ? $model->name : ''),
			array('class' => 'col-md-4 form-control', 'placeholder' => 'name',
				$this->data[ACTION_EDIT] ? '' : 'disabled')); ?>
		<?php if ($val->error('name')): ?>
			<span class="control-label"><?php echo $val->error('name')->get_message(); ?></span>
		<?php endif; ?>
	</div>
	<?php echo render('admin/user/_get_username', array(ACTION_EDIT => $this->data[ACTION_EDIT])); ?>
</fieldset>

app/views/blog/slug/index.php

<?php echo render(FOUNDATION_VIEW_INDEX_HEADER); // 検索VIEWの標準ヘッダー ?>
<div class="row">
	<div class="col-md-12">
		<?php echo Form::open(array("id" => "search-condition-form", "class" => "form-inline")); ?>
		<?php echo Form::csrf(); // CSRF 保護 ?>
		<div id="search-condition" class="row search-condition-area">
			<div class="col-md-8 search-condition-input-box">
				<div class="col-md-12">
					<?php echo render(FOUNDATION_VIEW_SEARCH_DISPLAY_COUNTS); // 表示件数制御標準部品 ?>
				</div>
				<div class="col-md-12">
					<span class="input-group search-condition-input search-condition-input-end">
						<span class="input-group-addon">名前</span>
						<?php echo Form::input('name', isset($view_search['name']) ? $view_search['name'] : '', array('class' => 'form-control')); ?>
					</span>
				</div>
			</div>
			<div class="col-md-4">
				<?php echo render(FOUNDATION_VIEW_SEARCH_ACTION); // 検索実行標準部品 ?>
			</div>
		</div>
		<?php echo Form::close(); ?>
	</div>
</div>
<div class="row">
	<div class="col-md-12">
		<?php if ($model): ?>
			<table class="table table-striped table-hover table-condensed" id="data_table">
				<thead>
				<tr>
					<th class="nowrap">名前 <span class="badge">ID</span></th>
					<th class="nowrap">更新者</th>
					<th class="nowrap"> </th>
				</tr>
				</thead>
				<tbody>
				<?php foreach ($model as $item): ?>
					<tr id="search-result">
						<td class="nowrap"><?php echo $item->name.' <span class="badge">'.$item->id.'</span>'; ?></td>
						<td class="nowrap"><?php echo $item->user->username.' <span class="badge">'.$item->user_id.'</span>'; ?></td>
						<td class="nowrap"><?php echo render('Accon::'.FOUNDATION_VIEW_MODEL_ACTIONS, array(VIEW_ACTION_KEY => $item->id)); // 検索結果モデルアクション標準部品 ?></td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>
</div>
<?php echo render(FOUNDATION_VIEW_INDEX_FOOTER); // 検索VIEWの標準フッター ?>
<script>
	$('#data_table').dataTable({
		"paging": false, // ページング停止
		"info": false,
		"bFilter": false, // 検索機能停止
		"aoColumnDefs": [{"bSortable": false, "aTargets": [2]}] // ソート除外
	});
</script>

解説は後日

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?