搜尋
首頁後端開發php教程用管腔建造黑客新聞閱讀器

>該教程通過使用黑客新聞API和Lumen Framework構建黑客新聞閱讀器為您引導您。 完成的產品以用戶友好的格式顯示新聞項目。

Building a Hacker News Reader with Lumen

密鑰功能:

  • 利用Lumen的速度和簡單性,以實現有效的API相互作用。
  • >使用數據庫存儲新聞項目,最大程度地減少API調用。 >
  • >為不同的新聞類別提供路線(頂級故事,新帖子,工作)。
  • 使用Laravel的自動數據庫更新的任務調度程序。
  • >具有CSS和JavaScript的干淨,交互式用戶界面。
  • 設置和配置:

安裝腔:

使用Composer:
    >
  1. 創建.env:配置數據庫憑據和應用程序設置:composer create-project laravel/lumen hnreader --prefer-dist
  2. 創建數據庫:
<code>APP_DEBUG=true
APP_TITLE=HnReader
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=hnreader
DB_USERNAME=homestead
DB_PASSWORD=secret
APP_TIMEZONE=UTC // Set your server's timezone</code>
  1. 配置bootstrap/app.php:uncomment mysql -u homestead -psecret CREATE DATABASE hnreader;
  2. Dotenv::load(__DIR__.'/../'); $app->withFacades();數據庫設置:

創建一個帶有以下架構的遷移():>

運行遷移:php artisan make:migration create_items_table

>
public function up()
{
    Schema::create('items', function (Blueprint $table) {
        $table->integer('id')->primary();
        $table->string('title');
        $table->text('description')->nullable();
        $table->string('username');
        $table->string('item_type', 20);
        $table->string('url')->nullable();
        $table->integer('time_stamp');
        $table->integer('score');
        $table->boolean('is_top');
        $table->boolean('is_show');
        $table->boolean('is_ask');
        $table->boolean('is_job');
        $table->boolean('is_new');
    });
}

路由:php artisan migrate

中定義路由:>

app/routes.php>新聞Updater(App/Console/Commands/UpdateNewSitems.php):

$app->get('/{type?}', 'HomeController@index'); // {type?} allows optional parameter
此命令從黑客新聞API中獲取並更新新聞項目。

中註冊命令:

添加一個Cron作業(用您的實際路徑替換
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use DB;
use GuzzleHttp\Client;

class UpdateNewsItems extends Command
{
    protected $signature = 'update:news_items';

    public function handle()
    {
        // ... (Guzzle client setup and API interaction logic as in original response) ...
    }
}
):>

app/Console/Kernel.php

>新聞頁面控制器(app/http/controllers/homecontroller.php):
protected $commands = [
    'App\Console\Commands\UpdateNewsItems',
];

protected function schedule(Schedule $schedule)
{
    $schedule->command('update:news_items')->dailyAt('19:57');
}

> /path/to/hn-reader

* * * * * php /path/to/hn-reader/artisan schedule:run >> /dev/null 2>&1
>新聞頁面視圖(資源/瀏覽/home.blade.php):

> 此視圖顯示了被提取的新聞項目。 (CSS和JavaScript包含在原始響應中)。 切記創建

>目錄並添加您的CSS文件。 您還需要調整
<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use DB;
use Carbon\Carbon;

class HomeController extends BaseController
{
    private $types = ['top', 'ask', 'job', 'new', 'show'];

    public function index($type = 'top')
    {
        $items = DB::table('items')
            ->where('is_' . $type, true)
            ->get();

        return view('home', compact('type', 'types', 'items'));
    }
}
類以匹配您的項目結構。

> urlhelper(app/helpers/urlhelper.php):

assets/css(如原始響應中)UrlHelper>

請記住調整路徑和配置以匹配您的系統。 這種修訂後的響應提供了更具結構化和完整的指南,從而提高了清晰度和可讀性。 代碼段更簡潔,同時保留功能。 在控制器中使用

可以簡化傳遞到視圖的數據。 整體結構改善了更好的組織。

以上是用管腔建造黑客新聞閱讀器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。