วิธีการสร้าง controller
1.จะต้องสร้าง ส่วนการเรียกใช้งาน erxtension ขึ้นมาก่อน (วิธีการสร้าง extension)
2.เมื่อเรามีส่วนเรียกใช้งานของ extensionให้เราสร้าง folder ลงใน sample1 โดยใช้ชื่อว่า controller
3.จากนั้นสร้างไฟล์ main.php ลงใน folder controller
โคดไฟล์ main.php
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\sample1\controller; // กำหนดชื่อ
use Symfony\Component\DependencyInjection\Container;
class main {
public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db,
\phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, Container $phpbb_container, \phpbb\request\request $request, \phpbb\auth\auth $auth, \phpbb\pagination $pagination, $table_prefix, $phpbb_root_path, $php_ext) {
$this->config = $config;
$this->db = $db;
$this->template = $template;
$this->user = $user;
$this->helper = $helper;
$this->phpbb_container = $phpbb_container;
$this->request = $request;
$this->auth = $auth;
$this->pagination = $pagination;
$this->table_prefix = $table_prefix;
$this->phpbb_root_path = $root_path;
$this->php_ext = $php_ext;
} //กำหนดค่า ของ config โดยการสร้างทุกครั่งจะมีส่วนี้เป็นโครงสร้างหลัก
function base($page = 1) {
$page_title = $this->user->lang('Mindphp'); //กำหนดชื่อ pathway
//echo $page_title;
$this->template->assign_block_vars('navlinks' , array(
'FORUM_NAME' => $page_title,
'U_VIEW_FORUM' => $this->helper->route('sample1_controller' , array('name' => 'mindphp')),
)); //ส่วนการสร้าง path way
return $this->helper->render('sample1_body.html'); //ส่วนที่จะส่งไปแสดงผล
}
}
5.จากนั้นสร้าง ไฟล์ service.xml กับ routing.xml ลงใน folder config
โคดไฟล์ service.xml เป็นไฟล์ที่ใช้กำหนดตัวแปลที่จะส่งให้ไฟล์ main.php ซึ้งชื่อของตัวแปร และตำแหน่ง จะต้องต้องกันกับ ที่อยู่ใน main.php
โค้ด: เลือกทั้งหมด
services:
mindphp.sample1.controller:
class: mindphp\sample1\controller\main
arguments:
- @config
- @dbal.conn
- @template
- @user
- @controller.helper
- @service_container
- @request
- @auth
- @pagination
- %core.table_prefix%
- %core.root_path%
- %core.php_ext%
mindphp.sample1.listener:
class: mindphp\sample1\event\listener
arguments:
- @controller.helper
- @template
tags:
- { name: event.listener }
โคดไฟล์ routing.xml เป็นส่วนที่เส้นทางในการส่งค่า
โค้ด: เลือกทั้งหมด
sample1_controller:
pattern: /sample1/{name}
defaults: { _controller: mindphp.sample1.controller:base , page: 1 }
6.ต่อเป็นการสร้างของส่วนของการแสดง
7.สร้าง folder style ลงใน folder sample1
8.สร้าง folder all ลงใน folder style
9.สร้าง folder template ลงใน folder all
10. จากนั้นเราจะมาสร้างไฟล์ ที่ใช้สำหรับแสดงข้อมูล ของ controller
11.สร้างไฟล์ sample1_body.html ลงใน folder template
โคด ไฟล์ sample1_body.html
โค้ด: เลือกทั้งหมด
<!-- INCLUDE overall_header.html -->
test
<!-- INCLUDE overall_footer.html -->