-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.html
More file actions
50 lines (47 loc) · 13.4 KB
/
controller.html
File metadata and controls
50 lines (47 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Controller | Pinatra</title>
<meta name="description" content="A PHP verison of Sinatra: a DSL for quickly creating web applications in PHP with minimal effort.">
<link rel="preload" href="/assets/css/0.styles.73742341.css" as="style"><link rel="preload" href="/assets/js/app.35633900.js" as="script"><link rel="preload" href="/assets/js/6.7a49fda2.js" as="script"><link rel="prefetch" href="/assets/js/4.dd1ee4a1.js"><link rel="prefetch" href="/assets/js/2.9834f0cc.js"><link rel="prefetch" href="/assets/js/3.2b86a6bc.js"><link rel="prefetch" href="/assets/js/5.ad87e3bc.js"><link rel="prefetch" href="/assets/js/7.490e94d8.js"><link rel="prefetch" href="/assets/js/8.3b2a41b2.js"><link rel="prefetch" href="/assets/js/9.cfda04a6.js"><link rel="prefetch" href="/assets/js/10.20f55d86.js"><link rel="prefetch" href="/assets/js/11.fbd446e0.js"><link rel="prefetch" href="/assets/js/12.7418dee3.js"><link rel="prefetch" href="/assets/js/13.111e6ced.js"><link rel="prefetch" href="/assets/js/14.78258348.js"><link rel="prefetch" href="/assets/js/15.73297d5e.js"><link rel="prefetch" href="/assets/js/16.63b4556e.js"><link rel="prefetch" href="/assets/js/17.c9391c34.js"><link rel="prefetch" href="/assets/js/18.584449ed.js"><link rel="prefetch" href="/assets/js/19.c8dececa.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.41d79f37.js">
<link rel="stylesheet" href="/assets/css/0.styles.73742341.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Pinatra</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="https://github.com/Pinatra/Pinatra" target="_blank" rel="noopener noreferrer" class="nav-link external">
Source
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/controller.html" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/controller.html" class="nav-link">简体中文</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="https://github.com/Pinatra/Pinatra" target="_blank" rel="noopener noreferrer" class="nav-link external">
Source
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Languages</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/controller.html" class="nav-link router-link-exact-active router-link-active">English</a></li><li class="dropdown-item"><!----> <a href="/zh/controller.html" class="nav-link">简体中文</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>Guide</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/" class="sidebar-link">Introduction</a></li><li><a href="/install.html" class="sidebar-link">Installation</a></li><li><a href="/routing.html" class="sidebar-link">Routing</a></li><li><a href="/controller.html" class="active sidebar-link">Controller</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/controller.html#the-basic-design-of-controllers-in-routing" class="sidebar-link">The basic design of controllers in routing</a></li><li class="sidebar-sub-header"><a href="/controller.html#using-the-controller" class="sidebar-link">Using the controller</a></li><li class="sidebar-sub-header"><a href="/controller.html#_1-setting-up-a-namespace" class="sidebar-link">1. Setting up a namespace</a></li><li class="sidebar-sub-header"><a href="/controller.html#_2-let-pinatra-know" class="sidebar-link">2. Let Pinatra know</a></li><li class="sidebar-sub-header"><a href="/controller.html#_3-try-it" class="sidebar-link">3. try it</a></li></ul></li><li><a href="/view.html" class="sidebar-link">Views</a></li><li><a href="/model.html" class="sidebar-link">Model</a></li><li><a href="/kernel.html" class="sidebar-link">Kernel details</a></li><li><a href="/benchmark.html" class="sidebar-link">Benchmark</a></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="controller"><a href="#controller" aria-hidden="true" class="header-anchor">#</a> Controller</h1> <p>As a minimalist framework that mimics Sinatra, Pinatra does not provide the <code>Controller</code> and <code>Model</code> base components by default, but can be implemented with a simple DIY.</p> <p>Below I will show how to use controllers in the Pinatra framework.</p> <h2 id="the-basic-design-of-controllers-in-routing"><a href="#the-basic-design-of-controllers-in-routing" aria-hidden="true" class="header-anchor">#</a> The basic design of controllers in routing</h2> <p>Pinatra routing provides a very simple way to call <code>Controller@method</code>, which is followed by the following simple steps:</p> <ol><li>Define the root namespace of the controller. The default is '\', which is the root space.</li> <li>When you get a response request like <code>Controller@method</code>, new a object, then call the method and pass in the parameters.</li></ol> <div class="language-php extra-class"><pre class="language-php"><code><span class="token comment">// Fake code</span>
<span class="token variable">$a</span> <span class="token operator">=</span> <span class="token variable">$baseNameSpace</span><span class="token punctuation">.</span><span class="token variable">$controller</span><span class="token punctuation">;</span> <span class="token comment">// '\NameSpace\HomeController';</span>
<span class="token variable">$b</span> <span class="token operator">=</span> <span class="token variable">$method</span><span class="token punctuation">;</span> <span class="token comment">// 'home'</span>
<span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token variable">$a</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token variable">$method</span><span class="token punctuation">(</span><span class="token variable">$a</span><span class="token punctuation">,</span> <span class="token variable">$b</span><span class="token punctuation">,</span> <span class="token variable">$c</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="using-the-controller"><a href="#using-the-controller" aria-hidden="true" class="header-anchor">#</a> Using the controller</h2> <p>It takes three steps to load the controller into Pinatra.</p> <h2 id="_1-setting-up-a-namespace"><a href="#_1-setting-up-a-namespace" aria-hidden="true" class="header-anchor">#</a> 1. Setting up a namespace</h2> <p>The main purpose of setting up a namespace is to autoload, which is to let us not need to manually require the file before calling the class.</p> <p>The PSR-4 specification is to make the namespace as the same structure of the folder and file name, the most trouble-free. The PSR-0 specification allows each file to be set internally, with the disadvantage of rerun <code>composer dump-autoload</code> for each modification.</p> <p>Pinatra recommends the PSR-4 autoloading specification as follows.</p> <blockquote><ol><li>New controllers folder at the same level as the public folder</li> <li>Modify composer.json to add the following:</li></ol> <div class="language-json extra-class"><pre class="language-json"><code><span class="token property">"autoload"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"psr-4"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"Controllers\\"</span><span class="token operator">:</span> <span class="token string">"controllers/"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre></div><ol start="3"><li>Run the command <code>composer dump-autoload</code></li></ol></blockquote> <h2 id="_2-let-pinatra-know"><a href="#_2-let-pinatra-know" aria-hidden="true" class="header-anchor">#</a> 2. Let Pinatra know</h2> <p>Add a line of code to <code>public/index.php</code>:</p> <div class="language-php extra-class"><pre class="language-php"><code>\<span class="token package">Pinatra<span class="token punctuation">\</span>Routing<span class="token punctuation">\</span>Router</span><span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token variable">$baseNamespace</span> <span class="token operator">=</span> <span class="token single-quoted-string string">'\\Controllers\\'</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="_3-try-it"><a href="#_3-try-it" aria-hidden="true" class="header-anchor">#</a> 3. try it</h2> <h3 id="creating-a-controller"><a href="#creating-a-controller" aria-hidden="true" class="header-anchor">#</a> Creating a controller</h3> <p>Create a <code>controllers/HomeController.php</code> file with the following contents:</p> <div class="language-php extra-class"><pre class="language-php"><code><span class="token php language-php"><span class="token delimiter important"><?php</span>
<span class="token keyword">namespace</span> <span class="token package">Controllers</span><span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name">HomeController</span>
<span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">home</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">echo</span> <span class="token double-quoted-string string">"Welcome to homepage!"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</span></code></pre></div><h3 id="create-a-route"><a href="#create-a-route" aria-hidden="true" class="header-anchor">#</a> Create a route</h3> <div class="language-php extra-class"><pre class="language-php"><code><span class="token function">get</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'home'</span><span class="token punctuation">,</span> <span class="token single-quoted-string string">'HomeController@home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="test-results"><a href="#test-results" aria-hidden="true" class="header-anchor">#</a> Test results</h3> <p>Visit <code>http://0.0.0.0:8000/home</code> to get the following page, success!</p> <p><img src="/assets/img/4.65262032.jpg" alt="controller"></p></div> <div class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">9/6/2018, 4:36:09 PM</span></div></div> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/routing.html" class="prev">
Routing
</a></span> <span class="next"><a href="/view.html">
Views
</a>
→
</span></p></div> </div> <!----></div></div>
<script src="/assets/js/6.7a49fda2.js" defer></script><script src="/assets/js/app.35633900.js" defer></script>
</body>
</html>