ÔõÑùʹÓÃLaravelʵÏÖ·ÖÒ³¹¦Ð§
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ÌṩÁËÐí¶àÏÖ´ú»¯µÄÌØÕ÷ºÍ±ãÓÚ¿ª·¢µÄ¹¤¾ß£¬ÉÁ¿ª·¢Õß¿ÉÒÔÔ½·¢¸ßЧµØ¹¹½¨ÍøÕ¾ºÍÓ¦ÓóÌÐò¡£ÔÚ¿ª·¢webÓ¦ÓÃÀú³ÌÖУ¬¾³£ÐèÒª½«ÅÌÎÊЧ¹û·Öҳչʾ£¬ÕâƪÎÄÕ½«ÏÈÈÝÔõÑùʹÓÃlaravelʵÏÖ·ÖÒ³¹¦Ð§¡£
Ò»¡¢·ÖÒ³¼ò½é
·ÖÒ³ÊÇÖ¸½«Ò»·Ý´ó×ÚÊý¾Ý²ð·Ö³É¶à¸öÒ³Ãæ¾ÙÐÐչʾµÄÒªÁ죬ͨ³£ÇéÐÎÏÂÎÒÃÇ»áÉèÖÃÏÔʾÿҳ¼¸¶àÌõÊý¾Ý£¬Êý¾ÝÁ¿Áè¼Ý¸ÃÖµ±ã»á×Ô¶¯·Ò³¡£ÕâÖÖ·½·¨¿ÉÒÔ´ó´óïÔÌÒ³ÃæµÄ¼ÓÔØʱ¼äºÍÕ¼Óôø¿í£¬ÈÃÓû§¸üÈÝÒ×µØÉó²éºÍµ¼º½Êý¾Ý¡£
¶þ¡¢Laravel·ÖÒ³²Ù×÷
LaravelÌṩÁËPaginatorÀàÀ´ÊµÏÖ·ÖÒ³¹¦Ð§¡£ÎÒÃÇ¿ÉÒÔ½«ÅÌÎÊЧ¹û¹¤¾ßת´ï¸øPaginatorµÄmakeÒªÁ죬²¢Ö¸¶¨Ã¿Ò³ÒªÏÔʾµÄÊý¾ÝÁ¿£¬¾Í¿ÉÒÔ»ñÈ¡·ÖҳʵÀý¡£ÔÚʵÀýÉÏŲÓÃrenderÒªÁ죬Laravel¼´¿É×Ô¶¯ÉúÒòËØÒ³Á´½Ó¡£
ÏÂÃæÎÒÃÇÀ´¿´ÏêϸµÄ´úÂëʵÏÖ¡£
×°ÖÃLaravel
ÔËÐÐÒÔÏÂÏÂÁî×°ÖÃLaravel£º
composer create-project --prefer-dist laravel/laravel blog
µÇ¼ºó¸´ÖÆ
½¨ÉèMysqlÊý¾Ý¿â±í
½¨ÉèÊý¾Ý¿â±íarticles£¬²¢²åÈëһЩÑù±¾Êý¾Ý£º
CREATE TABLE `articles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `body` text COLLATE utf8mb4_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
µÇ¼ºó¸´ÖÆ
INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 1', 'This is article 1 content', '2021-01-01 12:00:00', '2021-01-01 12:00:00'); INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 2', 'This is article 2 content', '2021-01-02 12:00:00', '2021-01-02 12:00:00'); INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 3', 'This is article 3 content', '2021-01-03 12:00:00', '2021-01-03 12:00:00'); INSERT INTO `articles` (`title`, `body`, `created_at`, `updated_at`) VALUES ('Article 4', 'This is article 4 content', '2021-01-04 12:00:00', '2021-01-04 12:00:00');
µÇ¼ºó¸´ÖÆ
½ç˵ArticleÄ£×Ó
ÔÚapp/ModelsĿ¼Ï½¨ÉèArticle.phpÎļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Article extends Model { use HasFactory; protected $fillable = ['title', 'body']; public function getDateAttribute() { return $this->created_at->format('Y-m-d'); } }
µÇ¼ºó¸´ÖÆ
½ç˵ArticlesController
ÔÚapp/Http/ControllersĿ¼Ï½¨ÉèArticlesController.phpÎļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Article; class ArticlesController extends Controller { public function index(Request $request) { $articles = Article::paginate(2); return view('articles.index', compact('articles')); } }
µÇ¼ºó¸´ÖÆ
½¨ÉèArticlesÊÓͼ
ÔÚresources/viewsĿ¼Ï½¨ÉèarticlesĿ¼£¬ÔÚ¸ÃĿ¼Ï½¨Éèindex.blade.phpÎļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺
@extends('layouts.app') @section('content') <div> <div> <div> @foreach($articles as $article) <div> <div> {{ $article->title }} </div> <div> <p>{{ $article->body }}</p> </div> <div> <small>{{ $article->date }}</small> </div> </div> @endforeach {{ $articles->links() }} </div> </div> </div> @endsection
µÇ¼ºó¸´ÖÆ
ÆÊÎö£º
Ê×ÏÈÎÒÃÇforeachÑ»·Õ¹Ê¾Ã¿Ò»ÆªÎÄÕ¡£
ʹÓÃ$article->titleºÍ$article->bodyÊôÐÔ´ÓArticleÄ£×ÓÖлñÈ¡ÎÄÕÂÎÊÌâºÍÕýÎÄ¡£
ÔÚ$article->dateÊôÐÔÖлñÈ¡ÎÄÕÂÐû²¼ÈÕÆÚ¡£
ŲÓÃ$articles->links()ÒªÁ죬Laravel»á×Ô¶¯ÎªÎÒÃÇÉúÒòËØÒ³Á´½Ó¡£
½ç˵ҳÃæÑùʽ
ÔÚpublic/cssĿ¼Ï½¨Éèapp.cssÎļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺
.card { margin-bottom: 20px; } .pagination { margin-top: 20px; }
µÇ¼ºó¸´ÖÆ
ÔÚresources/views/layoutsĿ¼ÏµÄÎļþapp.blade.phpÖУ¬¼ÓÈëÒÔÏ´úÂ룺
nbsp;html> getLocale()) }}"> <!-- Meta Tags --> <meta> <meta> <title>{{ config('app.name') }}</title> <!-- Styles --> <link> <nav> <div> <a> {{ config('app.name') }} </a> <button> <span></span> </button> <div> <ul></ul> <ul></ul> </div> </div> </nav> @yield('content') <script></script>
µÇ¼ºó¸´ÖÆ
ÆÊÎö£º
ÔÚapp.blade.phpÖмÓÈëÁËBootstrap4µÄCSSÑùʽ¡£
ÎÒÃÇ»¹ÒýÈëÁËBootstrap4µÄJSÎļþ£¬¿ÉÊÇÎÒÃDz¢²»ÐèÒª×Ô¼ºÊÖ¶¯ÒýÈ룬ÓÉÓÚLaravel×Ô´øÁËWebpack´ò°ü¹¤¾ß£¬Õâ¸ö¹¤¾ß¿ÉÒÔ×Ô¶¯½«ËùÐèµÄJSÎļþ´ò°üµ½public/js/app.jsÎļþÖС£
²âÊÔ·ÖÒ³¹¦Ð§
»á¼ûhttp://127.0.0.1:8000/articles¿ÉÒÔ¿´µ½ÎÒÃǵÄÎÄÕÂÁбíÒѾչʾ³öÀ´ÁË£¬²¢ÇÒÎÒÃÇ¿ÉÒÔ̫ͨ¹ýÒ³Á´½ÓµÄ·½·¨ÔÚ¶à¸öÒ³ÃæÖ®¼äÇл»¡£
ÔÚÕâÀïÐèҪעÖصÄÊÇ£¬ÎÒÃÇÔÚ¿ØÖÆÆ÷ÖÐŲÓÃpaginateÒªÁìʱ£¬´«ÈëµÄ²ÎÊý2´ú±íÁËÒ»Ò³ÒªÏÔʾ¼¸ÌõÊý¾Ý£¬ÈôÊÇÕâ¸öÖµÉèÖõùýС£¬ÔòÒ³ÃæÉϵķÖÒ³Á´½Ó»áÐí¶à£¬µ¼ÖÂÒ³ÃæÔÓÂÒ£»ÈôÊÇÕâ¸öÖµÉèÖõùý´ó£¬ÔòÒ³ÃæչʾµÄÊý¾ÝÁ¿»áºÜ´ó£¬³¤Ê±¼äÛÕ±ÕÇëÇó£¬Ó°ÏìÒ³ÃæµÄ·¿ªËÙÂÊ£¬ÉõÖÁ»áµ¼ÖÂÄÚ´æÒç³öµÈÎÊÌ⣬ÒÔÊÇҪƾ֤ÏÖÕæÏàÐÎÉóÉ÷ÉèÖá£
Èý¡¢×ܽá
LaravelÌṩÁËÇ¿Ê¢µÄPaginatorÀàÀ´ÊµÏÖ·ÖÒ³¹¦Ð§£¬Ö»ÐèҪʹÓýÏÉٵĴúÂë¼´¿ÉÇáËÉʵÏÖ¡£±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃPaginatorÀ´·ÖҳչʾMySQLÊý¾Ý¿âÖеÄÊý¾Ý¡£ÎÒÃÇ»¹Õ¹Ê¾ÁËÔõÑùÔÚ·ÖҳЧ¹ûÖÐʹÓÃÄ£×ÓÊôÐÔ¡£µ±ÎÒÃÇÐèÒªÔÚ¿ª·¢ÍøÕ¾ºÍÓ¦ÓóÌÐòʱչʾ´ó×ÚÊý¾Ýʱ£¬·ÖÒ³ÊDZز»¿ÉÉٵĹ¦Ð§£¬Ëü²»µ«¿ÉÒÔ¼«´óµÄÌá¸ßÓû§ÌåÑ飬»¹ÄÜÓÐÓõؼõÇáЧÀÍÆ÷µÄ¿ªÏú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravelʵÏÖ·ÖÒ³¹¦Ð§µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡