Laravel 5.7+ Beginner 笔记
如果以下内容有任何错误,欢迎指出!以下所有的笔记都是本人自己一边网上搜查一边自学的内容,目前正在尝试自己做一个full stack project用PHP来当后端。12/28/2018
文章目录
重点笔记
Eloquent is Laravel’s default ORM (object-relational mapper). Eloquent makes it painless to retrieve and store data in your database using clearly defined “models”. Usually, each Eloquent model corresponds directly with a single database table.
Eloquent model跟数据库table同一个意思
We do not have to explicitly tell the Eloquent model which table it corresponds to because it will assume the database table is the plural form of the model name.
例子:Model Name: Stock
Database/Schema name: stocks
In Laravel, all HTML templates are stored in the resources/views directory.
Laravel是一个PHP框架based on MVC(Model View Controller),因为PHP本身的codebase是没有任何architecture的,所以用laravel可以让程序员更方便(特别是比较大的project的时候)
Blade
在浏览许多laravel guide的时候还注意到一个叫blade.php的东西,blade是一个templating engine,所以当我们正在做有关于前端的东西的时候,blade有一个inheritance views的功能可以更加方便我们前端的设计。
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.
有三个需要知道的知识点
- @include
- @include is used for reusable HTML just like a standard PHP include. It does not have that parent/child relationship like @yield and @section.
- 相当于Java的import相当于Java的import
- @yield
- @yield is mainly used to define a section in a layout.
- The layout usually contains your HTML, head, body, header and footers. You define an area (@yield) within the layout that your pages which are extending the template will put their content into.
- 把一个区域规划出来好方便parent/child随时调用这部分的代码(HTML/CSS/layout的代码)
- @session
- When that layout is extended with @extends, you can define what goes in that section with the @section directive in your views.
- 相当于Java里面的implements继承关系的时候需要用到的关键词,不同的是可以inherit部分的代码(先定义好的session)而不需要继承一整个class
Laravel File Structure
Folder Name | Purpose |
---|---|
App | contain the most significant stuff, especially “http” folder that has controller, middleware, models and the ‘route.php’ file (this changed in Laravel 5.7) |
Bootstrap | needed for startup of Laravel |
Database | migration and seeds |
Config | configuration files, such as session configuration or authentication configuration |
Public | ‘.htaccess’, ‘robots.txt’, ‘favicon.ico’, and ‘index.php’ all these files play an important roles in keeping your project in search engines |
Resources | views, where the viewable PHP/HTML codes are stored |
Storage | cache and log files are kept here |
Test | unit testing purposes |
Vendor | for third party packages that Laravel uses |
Laravel RESTful structures
Name | Purpose |
---|---|
get() | get a request |
post() | send a request |
patch() | … |
put() | … |
delete() | … |
any() | register any route that responds to any HTTP verbs |
group | keep a group of routes to the same directory together, such as group([‘prefix]’=>‘home’) |
Named routes
假如想要用session来展示不同的内容给还没登录的用户
// reach the login page
Route::get('/', function() {
return link_to_route('session/create', 'Login');
});
// name route
Route::get('session/create', ['as'=>'create', 'use'=>'SessionController@create']);
// is equivalent to
Route::get('/', function(){
return route('create');
});
Commands
- 建立一个新的laravel application:
laravel new laravel-project
- 运行php:
php artisan serve
- 建立一个model
php artisan make:model newModel
- 建立一个controller
php artisan make:controller ModelController --resource
- Register for multiple routes
php artisan route:list
- Migrate到数据库能理解的代码:
php artisan make:migration create_newmodels_table
- Migrate当前migration folder里面所有的schema:
php artisan migrate
关于Model
建立完model之后,在app文件夹可以找到所有创建的models
在以下例子里面,$fillable里面的内容相当于mysql里面的column
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
protected $fillable = [
'name',
'count',
'price',
'description',
];
}
更多关于schema可读这里
关于Controller
blah.
关于Migration
运行了make migration的指令之后,在database/migration文件夹下会看到一个文件*_created_newmodels_table的文件,看起来可能是这样:
<?php
[...]
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->integer('count');
$table->integer('price');
$table->softDeletes();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
在up函数里面这个schema对应着mysql会创建一个products的table,里面有auto increment的id,string name,text description, etc… 的columns