多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

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

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

來源:程序員人生   發布時間:2015-04-08 08:33:03 閱讀次數:3975次

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

1.擴大包開發

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

@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

在業務邏輯代碼中需要使用

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

現在我們這里實現1個簡單的通知插件,先創建包:

$ php artisan workbench shiyanlou/notification --resources

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

修改下包里composer.json中的authors

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

在項目根目錄下履行:

$ php artisan dump-autoload

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

'ShiyanlouNotificationNotificationServiceProvider',

這步做完后啟動開發服務器

$ php artisan serve

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

現在我們在src/Shiyanlou/Notification下創建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次會話,就是從你打開閱讀器窗口到關閉。

修改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下創建1個名為Facades的文件夾,在Facades目錄下創建1個名為Notification.php的文件,修改:

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

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

為了方便我們的使用,我們在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);

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

簡單的擴大包開發就完成了。

2.Artisan擴大開發

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

$ php artisan command:make ExportUsersCommand

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

Artisan::add(new ExportUsersCommand);

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

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組命令數組,并由數組清單所組成。

下面開始編寫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()); } }

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

現在就能夠測試我們自己開發的命令了,先履行:

$ php artisan export:users

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

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

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

3.自適應效果

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

首頁

文章內容頁面

登錄頁面

文章管理頁面

編輯文章頁面

4.小結

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

終究版代碼下載:

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

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

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 真人毛片免费全部播放完整 | 午夜影视福利 | 伊人猫咪| 精品国产高清毛片 | 国产精品日韩欧美一区二区 | 国产激情久久久久影 | 久久经典免费视频 | 欧美精欧美乱码一二三四区 | 国内精品久久久久激情影院 | 日本不卡视频网站 | 国产在线高清不卡免费播放 | 欧美freesex10一|3| 久久综合九色综合97欧美 | aaaaaa级特色特黄的毛片 | 视频三区精品中文字幕 | 日本一级高清不卡视频在线 | 国内久久久久影院精品 | 性欧美视频videos6一9 | 2020中文字幕 | 欧美一区二区丝袜高跟鞋 | 波多野结衣三区 | 欧美一级日韩一级亚洲一级 | 欧美另类69xxxx| 国产一区二区不卡 | 日韩欧美亚洲国产 | 欧美日韩亚洲精品国产色 | 一级毛片在线不卡直接观看 | 最新中文字幕第一页 | 亚洲综合欧美日本另类激情 | 一级毛片ab片高清毛片 | 国产欧美在线观看不卡一 | 久久亚 | 国产最新一区二区三区天堂 | 成人国产精品久久久免费 | 免费看www网站入口 免费看w片的网站在线看 | 亚洲国产天堂久久综合2261144 | 性欧美另类老妇高清 | 免费视频爱爱 | 国产aaa免费视频国产 | 女人牲交一级毛片 | 交video|