可供学习的站点:一个简单的博客实现
http://www.uncletoo.com/html/application/773.html
上文是一个译文,其中的源代码在laravel4.2中已经无法使用,API函数有了一定的修改,请参考译文的原版blog
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-4-layout-views/
中的源码
Laravel是一个典型的MVC架构(MODEL, VIEW, CONTROLLER),代码非常漂亮简介。
笔者根据相应的学习资料,打算自己写一个类似的博客,但实现的功能略有不同。
该项目计划完成的功能(暂定):
实现基本的用户的注册、登陆、邮箱验证功能。
每个用户可以发表以图片为内容的主题,可以描述题目和内容。
每个主题可以被任意多个用户评论。
管理员可以对用户、主题、评论进行操作和维护。
第一步(安装):
1、安装Laravel 4。具体步骤及命令参考这里
第二部(配置):
1.笔者使用的php环境是XAMPP,用mysql先建一个名为piclist的database,直接在XAMPP Control Panel点击MySQL的admin选项即可手动配置数据库相关的事项。
2.进入网站根目录,进入app/config。
#app.php修改如key,dubug选项,时区等配置
<?php return array( /* |-------------------------------------------------------------------------- | Application Debug Mode |-------------------------------------------------------------------------- | | When your application is in debug mode, detailed error messages with | stack traces will be shown on every error that occurs within your | application. If disabled, a simple generic error page is shown. | */ 'debug' => true, /* |-------------------------------------------------------------------------- | Application URL |-------------------------------------------------------------------------- | | This URL is used by the console to properly generate URLs when using | the Artisan command line tool. You should set this to the root of | your application so that it is used when running Artisan tasks. | */ 'url' => 'http://localhost/', /* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. We have gone | ahead and set this to a sensible default for you out of the box. | */ 'timezone' => 'Asia/Shanghai', /* |-------------------------------------------------------------------------- | Application Locale Configuration |-------------------------------------------------------------------------- | | The application locale determines the default locale that will be used | by the translation service provider. You are free to set this value | to any of the locales which will be supported by the application. | */ 'locale' => 'en', /* |-------------------------------------------------------------------------- | Application Fallback Locale |-------------------------------------------------------------------------- | | The fallback locale determines the locale to use when the current one | is not available. You may change the value to correspond to any of | the language folders that are provided through your application. | */ 'fallback_locale' => 'en', /* |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | | This key is used by the Illuminate encrypter service and should be set | to a random, 32 character string, otherwise these encrypted strings | will not be safe. Please do this before deploying an application! | */ 'key' => 'Ov!WhEvJUCcoN!f3K6pQE7FzZJ$yDA#s', 'cipher' => MCRYPT_RIJNDAEL_128, /* |-------------------------------------------------------------------------- | Autoloaded Service Providers |-------------------------------------------------------------------------- | | The service providers listed here will be automatically loaded on the | request to your application. Feel free to add your own services to | this array to grant expanded functionality to your applications. | */ 'providers' => array( 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', 'Illuminate\Cache\CacheServiceProvider', 'Illuminate\Session\CommandsServiceProvider', 'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider', 'Illuminate\Routing\ControllerServiceProvider', 'Illuminate\Cookie\CookieServiceProvider', 'Illuminate\Database\DatabaseServiceProvider', 'Illuminate\Encryption\EncryptionServiceProvider', 'Illuminate\Filesystem\FilesystemServiceProvider', 'Illuminate\Hashing\HashServiceProvider', 'Illuminate\Html\HtmlServiceProvider', 'Illuminate\Log\LogServiceProvider', 'Illuminate\Mail\MailServiceProvider', 'Illuminate\Database\MigrationServiceProvider', 'Illuminate\Pagination\PaginationServiceProvider', 'Illuminate\Queue\QueueServiceProvider', 'Illuminate\Redis\RedisServiceProvider', 'Illuminate\Remote\RemoteServiceProvider', 'Illuminate\Auth\Reminders\ReminderServiceProvider', 'Illuminate\Database\SeedServiceProvider', 'Illuminate\Session\SessionServiceProvider', 'Illuminate\Translation\TranslationServiceProvider', 'Illuminate\Validation\ValidationServiceProvider', 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', ), /* |-------------------------------------------------------------------------- | Service Provider Manifest |-------------------------------------------------------------------------- | | The service provider manifest is used by Laravel to lazy load service | providers which are not needed for each request, as well to keep a | list of all of the services. Here, you may set its storage spot. | */ 'manifest' => storage_path().'/meta', /* |-------------------------------------------------------------------------- | Class Aliases |-------------------------------------------------------------------------- | | This array of class aliases will be registered when this application | is started. However, feel free to register as many as you wish as | the aliases are "lazy" loaded so they don't hinder performance. | */ 'aliases' => array( 'App' => 'Illuminate\Support\Facades\App', 'Artisan' => 'Illuminate\Support\Facades\Artisan', 'Auth' => 'Illuminate\Support\Facades\Auth', 'Blade' => 'Illuminate\Support\Facades\Blade', 'Cache' => 'Illuminate\Support\Facades\Cache', 'ClassLoader' => 'Illuminate\Support\ClassLoader', 'Config' => 'Illuminate\Support\Facades\Config', 'Controller' => 'Illuminate\Routing\Controller', 'Cookie' => 'Illuminate\Support\Facades\Cookie', 'Crypt' => 'Illuminate\Support\Facades\Crypt', 'DB' => 'Illuminate\Support\Facades\DB', 'Eloquent' => 'Illuminate\Database\Eloquent\Model', 'Event' => 'Illuminate\Support\Facades\Event', 'File' => 'Illuminate\Support\Facades\File', 'Form' => 'Illuminate\Support\Facades\Form', 'Hash' => 'Illuminate\Support\Facades\Hash', 'HTML' => 'Illuminate\Support\Facades\HTML', 'Input' => 'Illuminate\Support\Facades\Input', 'Lang' => 'Illuminate\Support\Facades\Lang', 'Log' => 'Illuminate\Support\Facades\Log', 'Mail' => 'Illuminate\Support\Facades\Mail', 'Paginator' => 'Illuminate\Support\Facades\Paginator', 'Password' => 'Illuminate\Support\Facades\Password', 'Queue' => 'Illuminate\Support\Facades\Queue', 'Redirect' => 'Illuminate\Support\Facades\Redirect', 'Redis' => 'Illuminate\Support\Facades\Redis', 'Request' => 'Illuminate\Support\Facades\Request', 'Response' => 'Illuminate\Support\Facades\Response', 'Route' => 'Illuminate\Support\Facades\Route', 'Schema' => 'Illuminate\Support\Facades\Schema', 'Seeder' => 'Illuminate\Database\Seeder', 'Session' => 'Illuminate\Support\Facades\Session', 'SoftDeletingTrait' => 'Illuminate\Database\Eloquent\SoftDeletingTrait', 'SSH' => 'Illuminate\Support\Facades\SSH', 'Str' => 'Illuminate\Support\Str', 'URL' => 'Illuminate\Support\Facades\URL', 'Validator' => 'Illuminate\Support\Facades\Validator', 'View' => 'Illuminate\Support\Facades\View', ), );
#database.php这里修改默认的数据库连接以及连接属性。这里选用mysql,以及默认的数据库是我们刚刚手动新建的piclist,用户名密码为自己mysql的配置,笔者的mysql仅为测试用,所以为无密码的root账户。
<?php return array( /* |-------------------------------------------------------------------------- | PDO Fetch Style |-------------------------------------------------------------------------- | | By default, database results will be returned as instances of the PHP | stdClass object; however, you may desire to retrieve records in an | array format for simplicity. Here you can tweak the fetch style. | */ 'fetch' => PDO::FETCH_CLASS, /* |-------------------------------------------------------------------------- | Default Database Connection Name |-------------------------------------------------------------------------- | | Here you may specify which of the database connections below you wish | to use as your default connection for all database work. Of course | you may use many connections at once using the Database library. | */ 'default' => 'mysql', /* |-------------------------------------------------------------------------- | Database Connections |-------------------------------------------------------------------------- | | Here are each of the database connections setup for your application. | Of course, examples of configuring each database platform that is | supported by Laravel is shown below to make development simple. | | | All database work in Laravel is done through the PHP PDO facilities | so make sure you have the driver for your particular database of | choice installed on your machine before you begin development. | */ 'connections' => array( 'sqlite' => array( 'driver' => 'sqlite', 'database' => __DIR__.'/../database/production.sqlite', 'prefix' => '', ), 'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'piclist', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), 'pgsql' => array( 'driver' => 'pgsql', 'host' => 'localhost', 'database' => 'forge', 'username' => 'forge', 'password' => '', 'charset' => 'utf8', 'prefix' => '', 'schema' => 'public', ), 'sqlsrv' => array( 'driver' => 'sqlsrv', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => '', 'prefix' => '', ), ), /* |-------------------------------------------------------------------------- | Migration Repository Table |-------------------------------------------------------------------------- | | This table keeps track of all the migrations that have already run for | your application. Using this information, we can determine which of | the migrations on disk haven't actually been run in the database. | */ 'migrations' => 'migrations', /* |-------------------------------------------------------------------------- | Redis Databases |-------------------------------------------------------------------------- | | Redis is an open source, fast, and advanced key-value store that also | provides a richer set of commands than a typical key-value systems | such as APC or Memcached. Laravel makes it easy to dig right in. | */ 'redis' => array( 'cluster' => false, 'default' => array( 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ), ), );
关于数据库表的建立,这里不使用手动建立的方法了,laravel提供了强大的数据库版本管理功能,不但可以以php语言简单地实现数据库建表工作,还可以通过数据库迁移,明确了数据库版本更新的规则,给未来回滚或者重置提供了方法。
在cmd(windows系统下)中,运行php artisan migrate:make create_tablename_table,tablename是所要建立的表名,这里运行三个命令,我们的博客需要建立三张表:
>php artisan migrate:make create_users_table >php artisan migrate:make create_pictures_table >php artisan migrate:make create_comments_table
这时候进入app\database\migrations,会发现带上了时间戳和表名的三个php文件。
接下来,我们就要在这三个文件书写我们的建表规定,为了满足我们的需求,需要的建表语言如下:
1#......xxxx_xx_xx_xxxxxx_create_users_table.php这里定义我们要创建的用户数据表,数据库相关的内容都可以在laravel mannual中找到,多读文档对编写代码非常有帮助。
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('username'); $table->string('password'); $table->string('email'); $table->string('remember_token'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('users', function (Blueprint $table) { $table->drop(); }); } }
2#......xxxx_xx_xx_xxxxxx_create_pictures_table.php这里定义我们的图片表,图片是该博客的主内容,它由唯一一个用户所发,可被任意多个用户评论,$table->binary()函数可以指定一个mysql中blob类型的项,用它可以存储图片数据(此处已修改,不直接存储图片到数据库,而是存图片的路径)。
当一个新图片被上传时,首先在服务端以时间戳和文件名为信息唯一创建一个图片文件,将它的路径存入数据库,放置了数据库端的大量读写。
图片作为一个主题,支持全局搜索,可以搜索标题或者描述中出现的词。
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePicturesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('pictures', function(Blueprint $table){ $table->increments('id'); $table->string('title'); $table->text('description'); $table->binary('image'); $table->unsignedInteger('user_id'); $table->unsignedInteger('comment_count'); $table->timestamps(); $table->engine = 'MyISAM'; }); DB::statement('ALTER TABLE pictures ADD FULLTEXT search(title, description)'); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('pictures', function (Blueprint $table) { $table->dropIndex('search'); $table->drop(); }); } }
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreateCommentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('picture_id'); $table->string('commenter'); $table->string('email'); $table->text('comment'); $table->boolean('approved'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('comments'); } }
php artisan migrate此时查看MySQL数据库,发现建立了三个数据表+一个数据迁移表,分别是我们创建的网页需要的表,和一张表示我们数据库迁移记录的表。
(注:每当我们需要对数据库的结构进行改变,就可以使用artisan命令创建一个迁移文件,定义我们的迁移过程,比如新建属性,修改表名等等,然后应用迁移命令进行数据库结构的改变,这样的习惯有助于当我们需要回滚数据库的历史版本时,可以更方便地操作和维护,具体可以参看laravel手册)