<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP &#8211; nood1es</title>
	<atom:link href="https://yijie.lu/category/code/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://yijie.lu</link>
	<description>Rick&#039;s Blog</description>
	<lastBuildDate>Thu, 18 May 2017 06:53:44 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.4</generator>

<image>
	<url>https://yijie.lu/wp-content/uploads/2017/05/411.png</url>
	<title>PHP &#8211; nood1es</title>
	<link>https://yijie.lu</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>基于CodeIgniter的Service层构建</title>
		<link>https://yijie.lu/service-layer-4-codeigniter/</link>
				<comments>https://yijie.lu/service-layer-4-codeigniter/#respond</comments>
				<pubDate>Tue, 18 Apr 2017 04:07:46 +0000</pubDate>
		<dc:creator><![CDATA[Rick Lu]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[CodeIgniter]]></category>

		<guid isPermaLink="false">https://yijie.lu/?p=8</guid>
				<description><![CDATA[<p>最近做了一些事情，其中一件就是分离出之前基于CI写的Service层并打包发布到GitHub。 想着还是应该留 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://yijie.lu/service-layer-4-codeigniter/">基于CodeIgniter的Service层构建</a> appeared first on <a rel="nofollow" href="https://yijie.lu">nood1es</a>.</p>
]]></description>
								<content:encoded><![CDATA[<p>最近做了一些事情，其中一件就是分离出之前基于CI写的Service层并打包发布到GitHub。<br />
想着还是应该留下一点什么，便是来由。<br />
这是还在Yumeer的时候，那会儿江哥刚来，梳理了整个业务逻辑，发现杂乱无章，遂推倒了重来。还是我一人负责后端，囿于经验，之前完全是hard-coding，代码复用性基本为0，加之CI的特性：</p>
<blockquote><p>-The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.<br />
-The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of “page”.<br />
-The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.</p></blockquote>
<p><span id="more-8"></span></p>
<p>导致大量business logic堆积到Controller层甚至Model层，着实难看。后面江哥提出一个想法，做Microservice，了解过后，准备开做。<br />
CI的简洁易用性出于此，麻烦也同出于此。由于Controller层无法互相调用，在设计模式上，只能通过新构建一层架于C-M之间。本着面向Google/Stackoverflow编程的原则（手动滑稽），先找了找，发现了一些思路和代码片段。遂开始仔细分析。<br />
CI提供了极其灵活的扩展接口，分析过后，找到一条路，简而言之就是通过对core的扩展，增加一层类似于library层的独立层，同样使用原生load进行加载。</p>
<p>核心代码：</p>
<pre class="EnlighterJSRAW" data-enlighter-language="null">// The service layer core
public function service($service = '', $params = NULL, $object_name = NULL)
{
    if (is_array($service)) {
        foreach ($service as $class) {
            $this-&gt;service($class, $params);
        }
        return $this;
    }
    if ($service == '' or isset($this-&gt;_ci_services[$service])) {
        return FALSE;
    }
    if (!is_null($params) &amp;&amp; !is_array($params)) {
        $params = NULL;
    }
    $subdir = '';

// Is the service in a sub-folder? If so, parse out the filename and path.
    if (($last_slash = strrpos($service, '/')) !== FALSE) {
// The path is in front of the last slash
        $subdir = substr($service, 0, $last_slash + 1);
// And the service name behind it
        $service = substr($service, $last_slash + 1);
    }
    foreach ($this-&gt;_ci_service_paths as $path) {
        $filepath = $path . 'services/' . $subdir . $service . '.php';
        if (!file_exists($filepath)) {
            continue;
        }
        include_once($filepath);
        $service = strtolower($service);
        if (empty($object_name)) {
            $object_name = $service;
        }
        $service = ucfirst($service);
        $CI =&amp; get_instance();
        if ($params !== NULL) {
            $CI-&gt;$object_name = new $service($params);
        } else {
            $CI-&gt;$object_name = new $service();
        }
        $this-&gt;_ci_services[] = $object_name;
        return $this;
    }
}</pre>
<pre class="EnlighterJSRAW" data-enlighter-language="null">// Extend load
class MY_Service
{
    public function __construct()
    {
        log_message('debug', "Service Class Initialized");
    }
    function __get($key)
    {
        $CI =&amp; get_instance();
        return $CI-&gt;$key;
    }
}</pre>
<p>&nbsp;</p>
<p>Flowchart：<br />
待补<br />
GitHub：<br />
<a href="https://github.com/acerest/ServiceLayer4CodeIgniter">acerest/ServiceLayer4CodeIgniter</a><br />
欢迎Star ：）</p>
<p>The post <a rel="nofollow" href="https://yijie.lu/service-layer-4-codeigniter/">基于CodeIgniter的Service层构建</a> appeared first on <a rel="nofollow" href="https://yijie.lu">nood1es</a>.</p>
]]></content:encoded>
							<wfw:commentRss>https://yijie.lu/service-layer-4-codeigniter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
							</item>
	</channel>
</rss>
