最近更新: 2024-07-21

PHP框架 - CommonGateway HTML公用函數與預設首頁

CommonGateway 是我多年前設計的迷你框架。我最初用它實作 RESTful API 項目,並沒有放太多用於網頁設計的內容。最近兩年用它設計一些網站專案。為了方便工作,增加了兩項關於網頁設計的功能:

  • HTML 公用函數
  • 預設首頁控制項

HTML 公用函數

CommonGateway 這一年重構了一些程式碼,導入名稱空間 (namespace) 。規劃 cg\html 這一塊名稱空間作為網站設計公用函數的空間。這些函數主要用在 HTML 視圖 (.phtml),目的是提供正確的資源 URL 。

這些函數都能接受多個參數,可選擇用變動長度參數清單或者放入陣列。

  • request_url($controller_path = null, …$args) 取得基於 index.php 的控制項 URL 字串。
    • request_url() = “//HOST/index.php”
    • request_url(‘control’) = “//HOST/index.php/control”
    • request_url(‘control’, 123, ‘abc’) = “//HOST/index.php/control/123/abc”
    • request_url(‘control’, [123, ‘abc’]) = “//HOST/index.php/control/123/abc”
  • home_url() 取得首頁的URL。
  • redirect($controller_path = false, …$args) 導向到指定的控制項。若省略控制項就是回到首頁(index.php)。 實際上就是執行 header(‘Location: ‘ . request_url($controller_path));
  • resource_url(…$path_segments) 取得網站指定資源的 URL (URL中不會包含 index.php)。
  • stylesheet(…$srcs) 顯示 CSS 文件的 HTML 載入代碼。可接受多個 css 檔案路徑。
    • stylesheet(‘style1.css’, ‘style2.css’)
    • stylesheet([‘style1.css’, ‘style2.css’])
  • script(…$srcs) 顯示 JavaScript 文件的 HTML 載入代碼。可接受多個 js 檔案路徑。
  • refresh($seconds) 指示 HTML 網頁的定期更新週期。內容是 <meta http-equiv="refresh" content="$seconds">

完整列表請看 CommonGateway 功能文件: HTML 公用函數

範例:


<head>
<meta charset="utf-8">
<title>cg\html functions demo</title>

<?php
cg\html\stylesheet([
  'css/bootstrap.min.css',
  'css/theme/base.css',
  'css/theme/dark.css'
]);
?>
</head>

<body>
<div>
<img src="<?=cg\html\resource_url('images/logo.png')?>">
</div>

<p>goto: <a href="<?=cg\html\home_url()?>">home</a>.
</p>

<p>goto: <a href="<?=cg\html\request_url('profile')?>">profile</a>.
</p>

<?php
cg\html\script([
    'js/jquery-3.3.1.slim.min.js',
    'js/popper.min.js',
    'js/bootstrap.min.js'
    ]);
?>
</body>
</html>

假設 index.php 的 URL 是 http://your_host/myweb/index.php ,則上例視圖的實際產出內容如下:


<head>
<meta charset="utf-8">
<title>cg\html functions demo</title>

<link rel="stylesheet" href="//your_host/myweb/css/bootstrap.min.css">
<link rel="stylesheet" href="//your_host/myweb/css/theme/base.css">
<link rel="stylesheet" href="//your_host/myweb/css/theme/dark.css">
</head>

<body>
<div>
<img src="//your_host/myweb/images/logo.png">
</div>

<p>goto: <a href="//your_host/myweb/index.php">home</a>.
</p>

<p>goto: <a href="//your_host/myweb/index.php/profile">profile</a>.
</p>

<script src="//your_host/myweb/js/jquery-3.3.1.slim.min.js"></script>
<script src="//your_host/myweb/js/popper.min.js"></script>
<script src="//your_host/myweb/js/bootstrap.min.js"></script>
</body>
</html>

預設首頁

CommonGateway 有兩種方式建立預設首頁。

  1. 建立 views/index.phtml 。範例 default-index-page
  2. 建立一個名為 Home 的控制項(controller): 定義 Home 類別,實作 index 方法。範例 default-home-controller;

當使用者執行 CommonGateway 但沒有指定控制項時,若有 views/index.phtml 或 Home 控制項就會執行它。讓使用者不必輸入 your_site/index.php/home 這種 URL 。

如果你正利用 CommonGateway Framework 設計一組 RESTful API 服務,可以用第一種方式作 API 服務的索引頁。

當你用 CommonGateway Framework 設計一個網站時,建議用第二種方式建立你的網站首頁。此途徑可以使用 CommonGateway Framework 提供的所有資源。例如 @resource 、 @authorize 以及控制項的資源。

相關文章