日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當前位置:首頁 > php開源 > 綜合技術(shù) > Laravel大型項目系列教程(七)之7 擴展包和Artisan開發(fā)

Laravel大型項目系列教程(七)之7 擴展包和Artisan開發(fā)

來源:程序員人生   發(fā)布時間:2015-04-08 08:33:03 閱讀次數(shù):3977次

本節(jié)教程將講授擴大包開發(fā)和Artisan擴大開發(fā),并閱讀不同分辨率下的自適應效果。本節(jié)結(jié)束后全部教程就結(jié)束了,文章最后有完全版程序代碼的下載。

1.擴大包開發(fā)

在前面開發(fā)中,我們常常要用到通知,如修改用戶信息時視圖要寫

@if (Session::has('message')) <div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert> <p>{{ Session::get('message')['content'] }}</p> </div> @endif

在業(yè)務(wù)邏輯代碼中需要使用

return Redirect::route('user.edit', $id)->with('user', $user)->with('message', array('type' => 'success', 'content' => 'Modify successfully'));

現(xiàn)在我們這里實現(xiàn)1個簡單的通知插件,先創(chuàng)建包:

$ php artisan workbench shiyanlou/notification --resources

這時候會在項目根目錄下多1個名為workbench的目錄,里面寄存的就是剛才創(chuàng)建的包,我們進入shiyanlou/notification目錄,src/Shiyanlou/Notification目錄是所有class的主目錄,包括ServiceProvider。config、lang、migrations和views目錄,就如你所猜想,包括了你創(chuàng)建的包的相應資源。包可以包括這些資源中的任意幾個,就像1個”常規(guī)”的利用。

修改下包里composer.json中的authors

"authors": [ { "name": "shiyanlou", "email": "support@shiyanlou.com" } ]

在項目根目錄下履行:

$ php artisan dump-autoload

然后我們在app/config/app.php中的providers中增加:

'ShiyanlouNotificationNotificationServiceProvider',

這步做完后啟動開發(fā)服務(wù)器

$ php artisan serve

如果啟動成功,就說明擴大包的基礎(chǔ)就搭建完成了。

現(xiàn)在我們在src/Shiyanlou/Notification下創(chuàng)建1個名為Notification.php的文件,修改:

<?php namespace ShiyanlouNotification; use IlluminateSessionStore as SessionStore; class Notification { private $session = null; public function __construct(SessionStore $session) { $this->session = $session; } private function addMessage($type, $content) { $this->session->put('notification_message', '<div class="am-alert ' . $type . '" data-am-alert><p></p>' . $content . '</div>'); } public function primary($content) { $this->addMessage('am-alert-primary', $content); } public function secondary($content) { $this->addMessage('am-alert-secondary', $content); } public function success($content) { $this->addMessage('am-alert-success', $content); } public function warning($content) { $this->addMessage('am-alert-warning', $content); } public function danger($content) { $this->addMessage('am-alert-danger', $content); } public function show() { echo $this->session->pull('notification_message', ''); } }

上面用到了Session,Session表示1次會話,就是從你打開閱讀器窗口到關(guān)閉。

修改NotificationServiceProvider.php中的register()provides()

public function register() { $this->app['notification'] = $this->app->share(function($app) { return new Notification($this->app['session.store']); }); } public function provides() { return array('notification'); }

上面是向Ioc容器注冊類。

然后在src/Shiyanlou/Notification下創(chuàng)建1個名為Facades的文件夾,在Facades目錄下創(chuàng)建1個名為Notification.php的文件,修改:

<?php namespace ShiyanlouNotificationFacades; use IlluminateSupportFacadesFacade; class Notification extends Facade { protected static function getFacadeAccessor() { return 'notification'; } }

我們這里繼承了Facade類,用Facades可以訪問IoC容器中注冊的類,有了IoC容器,我們可以在任何地方調(diào)用注冊的類。

為了方便我們的使用,我們在app/config/app.phpaliases中增加1個別名:

'Notification' => 'ShiyanlouNotificationFacadesNotification',

下面就來試試這個插件,把上面的

@if (Session::has('message')) <div class="am-alert am-alert-{{ Session::get('message')['type'] }}" data-am-alert> <p>{{ Session::get('message')['content'] }}</p> </div> @endif

替換成

{{ Notification::show() }}

return Redirect::route('user.edit', $id)->with('user', $user)->with('message', array('type' => 'success', 'content' => 'Modify successfully'));

替換成

Notification::success('Modify successfully'); return Redirect::route('user.edit', $id);

現(xiàn)在修改用戶信息后提示成功的信息就可以方便地顯示出來:

簡單的擴大包開發(fā)就完成了。

2.Artisan擴大開發(fā)

Artisan是Laravel中自帶的命令行工具的名稱,它提供了1些開發(fā)進程中有用的命令。我們可以編寫自己的Artisan命令完成特定的功能,這里舉1個開發(fā)導出用戶數(shù)據(jù)的命令。首先我們創(chuàng)建1個新的命令類:

$ php artisan command:make ExportUsersCommand

履行完后我們會發(fā)現(xiàn)在app/commands生成了1個ExportUsersCommand.php的文件,這個就是我們自定義的命令類,然后我們需要注冊命令,在app/start/artisan.php中增加:

Artisan::add(new ExportUsersCommand);

下面編寫ExportUsersCommand類,把$name的值改成export:users,這個$name是命令的名稱,把$description的值改成Export all users,這個是命令的描寫,然后添加1個獲得用戶數(shù)據(jù)的方法:

protected function getUsersData() { $users = User::all(); foreach ($users as $user) { $output[] = [$user->id, $user->email, $user->nickname, $user->is_admin, $user->block, $user->created_at]; } return $output; }

然后編寫getArguments()getOptions()

protected function getArguments() { return array( array('file', InputArgument::OPTIONAL, 'The output file path', null), ); } protected function getOptions() { return array( array('headers', null, InputOption::VALUE_NONE, 'Display headers?', null), ); }

getArgumentsgetOptions方法是用來接收要傳入您的自定義命令的地方,這兩個方法都會回傳1組命令數(shù)組,并由數(shù)組清單所組成。

下面開始編寫fire()

public function fire() { $output_path = $this->argument('file'); $headers = ['ID', 'E-mail', 'NickName', 'is_admin', 'is_block', 'CreateDateTime']; $rows = $this->getUsersData(); if ($output_path) { $handle = fopen($output_path, 'w'); if ($this->option('headers')) { fputcsv($handle, $headers); } foreach ($rows as $row) { fputcsv($handle, $row); } fclose($handle); $this->info("Exported list to $output_path"); } else { $table = $this->getHelperSet()->get('table'); $table->setHeaders($headers)->setRows($rows); $table->render($this->getOutput()); } }

當自定義命令被履行時,將會調(diào)用fire方法,你可以在此加入任何的邏輯判斷。

現(xiàn)在就能夠測試我們自己開發(fā)的命令了,先履行:

$ php artisan export:users

履行后會在命令行終端輸出用戶列表,我們試試導出到1個文件:

$ php artisan export:users --headers users.csv

履行后終端會輸出Exported list to users.csv,在項目根目錄下會生成1個名為users.csv的文件,你可以用表格軟件或直接打開,里面寄存的就是用戶的數(shù)據(jù)列表。

3.自適應效果

讓我們看下在低分辨率下的自適應效果

首頁

文章內(nèi)容頁面

登錄頁面

文章管理頁面

編輯文章頁面

4.小結(jié)

本節(jié)教程介紹了怎樣進行擴大包和Artisan開發(fā),本套教程也就此結(jié)束了,你可以繼續(xù)完善這個博客,此教程僅僅只是做1個引入人,你完全可以用Laravel開發(fā)自己想要的網(wǎng)站,Laravel中的緩沖、Mail、本地化和隊列等還沒有提到,這就需要你自己去探索了,最后推薦1個開發(fā)環(huán)境Laravel Homestead,我們可以非常方便地在其中開發(fā)Laravel。

終究版代碼下載:

$ git clone https://github.com/shiyanlou/laravel-blog⑺-final.git

本文詳細出自http://www.shiyanlou.com/courses/123,轉(zhuǎn)載請注明出處

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 成人av在线影院 | 欧美日韩国产一区二区三区 | 黄色三级视频 | 久久人人爽人人爽人人片av不 | 日韩在线一区二区 | www久久久久 | 91精品国产91久久综合桃花 | 99精品国产福利在线观看免费 | 成人午夜久久 | 久久成人精品视频 | 中文字幕电影在线观看 | 久久精品资源 | 日韩三级视频在线观看 | 免费福利在线视频 | 国产呦精品一区二区三区网站 | 欧美高清二区 | 欧美一级免费大片 | 粉嫩av亚洲一区二区图片 | 欧美大片一区二区三区 | 精品视频99 | 欧美一区视频 | 99视频在线免费观看 | 韩国福利一区 | 91短视频在线看 | а√最新版天堂中文在线 | 亚洲欧美日韩久久精品 | 国产一二三区在线 | 日韩亚洲欧美一区 | 国产精品久久久久9999 | 日韩精品一二三区 | 国产综合激情 | 婷婷不卡 | 免费看成人吃奶视频在线 | 国产精品久久久久久久免费软件 | ...99久久国产成人免费精品 | 精品久久亚洲 | 在线观看二区 | 久久免费视频网 | 色姑娘综合网 | 欧美亚洲综合网 | 国产中文在线视频 |