關(guān)于laravel的介紹就不講了,總之laravel是款比較強(qiáng)大的框架,它是國(guó)外框架所以在安裝的上面可能比較麻煩。
laravel的安裝
首先安裝laravel之前要安裝composer,如果是linux系統(tǒng)即可直接下載安裝,下載完后不能安裝記得修改下文件權(quán)限用命令chmod,這邊主要講下window下如何使用composer這個(gè)工具。?
首先百度搜索中國(guó)composer鏡像,就可以找到composer config -g repositories.packagist composer?
http://packagist.phpcomposer.com這條命令,運(yùn)行cmd在命令行運(yùn)行上面的命令,就可以下載composer工具,
下載成功后可以看到composer文件底下有個(gè)composer.json文件這是一個(gè)配置文件,打開(kāi)配置文件寫(xiě)明php版本信息和要下載的laravel信息,格式如下:
{ "name": "laravel/laravel", "description": "The Laravel Framework.", "keywords": ["framework", "laravel"], "license": "MIT", "type": "project", "require": { "php": ">=5.5.9", "laravel/framework": "5.1.*" }, "require-dev": { "fzaninotto/faker": "~1.4", "mockery/mockery": "0.9.*", "phpunit/phpunit": "~4.0", "phpspec/phpspec": "~2.1" }, "autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/" } }, "autoload-dev": { "classmap": [ "tests/TestCase.php" ] }, "scripts": { "post-install-cmd": [ "php artisan clear-compiled", "php artisan optimize" ], "pre-update-cmd": [ "php artisan clear-compiled" ], "post-update-cmd": [ "php artisan optimize" ], "post-root-package-install": [ "php -r \"copy('.env.example', '.env');\"" ], "post-create-project-cmd": [ "php artisan key:generate" ] }, "config": { "preferred-install": "dist" }, "repositories": [ {"type": "composer", "url": "http://packagist.phpcomposer.com"}, {"packagist": false} ] }```
配置好之后輸入composer install 進(jìn)行安裝laravel,這邊要比較注意的是安裝目錄的路徑問(wèn)題,如果你想安裝在d盤(pán)底下就在把命令行切到d目錄底下進(jìn)行安裝(在此操作之前要配置好環(huán)境變量)。
laravel的目錄結(jié)構(gòu)介紹
安裝完的第一次肯定是要想怎么去運(yùn)行它,很簡(jiǎn)單,直接進(jìn)入public文件就可以打開(kāi)一個(gè)開(kāi)始頁(yè)面,如果在本地的話(huà)那就是localhost/laravelproject/public,就可以運(yùn)行。
接下來(lái)介紹下laravel目錄結(jié)構(gòu),首先介紹下public的index.php文件 里面主要是加載了開(kāi)始文件然后才能成功運(yùn)行l(wèi)aravel,具體的兩個(gè)文件你可以在根目錄下bootstrap文件夾中找到?,F(xiàn)在看下app中的結(jié)構(gòu):
view中主要放的是視圖文件(創(chuàng)建文件時(shí)要用到blade模板,比如創(chuàng)建test.blade.php,laravel中是結(jié)合blade模板引擎來(lái)調(diào)用視圖模板)
controller放的是控制器(手動(dòng)創(chuàng)建時(shí)記得要用composer 命令進(jìn)行更新)
config中主要是配置文件(比如配置數(shù)據(jù)庫(kù)時(shí)要用到database.php文件)
models主要是放模型(也就是數(shù)據(jù)庫(kù)的表)
routes則是路由配置,
filters則是過(guò)濾器。
laravel是怎么運(yùn)行的
剛學(xué)習(xí)時(shí)肯定是要先嘗試下如何運(yùn)行這個(gè)laravel,首先手動(dòng)創(chuàng)建一個(gè)controller,文件命名為T(mén)estController.php,然打開(kāi)命令行進(jìn)入項(xiàng)目的根目錄下 執(zhí)行 composer dumpautoload,里面內(nèi)容可以模仿homeController.php。
然后編輯routes.php文件,將原來(lái)的Route::GET(‘/’,function()…);修改為Route::Get(‘/’,’TestController@showWelcome’); 然后運(yùn)行也會(huì)跳到laravel歡迎界面。
如果Route::Get(‘test’,’TestController@showWelcome’);則在網(wǎng)站根目錄下后面直接增加test就可以訪(fǎng)問(wèn)了,到了這里應(yīng)該明白了怎么到Controller,Controller怎么到View了。
laravel數(shù)據(jù)庫(kù)配置
這邊用到的是mysql,進(jìn)行了簡(jiǎn)單的配置
'mysql' => array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'oss', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', )
laravel的數(shù)據(jù)庫(kù)使用
<?php use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface; class User extends Eloquent implements UserInterface, RemindableInterface { use UserTrait, RemindableTrait; /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = array('password', 'remember_token'); }
即可以直接使用 User ::all() 查詢(xún)所有結(jié)果 ,User::find(2)查詢(xún)一個(gè),Post::findOrFail(2)
如果沒(méi)找到就會(huì)返回錯(cuò)誤,Post::save()、Post::where()->find()、Post::add()、Post::delete()
數(shù)據(jù)庫(kù)的簡(jiǎn)便操作:
DB::table(‘tablename’)->insert([ 插入多個(gè)時(shí)要再加一個(gè)數(shù)組 ['title'=>'title','name'=>'name'] ['title'=>'title'] ['title'=>'title'] ]) 插入時(shí)要想得到ID DB::table('tablename')->insertGetId(['title'=>'titles']) 更新數(shù)據(jù)要有ID DB::table('tablename')->where('id',1)->update(['title'=>'titles']) 刪除數(shù)據(jù) DB::table('tablename')->where('id',1)->delete(); 查詢(xún)數(shù)據(jù) DB::table('tablename')->get(); 得到全部的值 DB::table('tablename')->get(['title']); 只查詢(xún)title的值 DB::table('tablename')->first(); 只拿第一個(gè) DB::table('tablename')->orderBy('id','desc')->first(); 根據(jù)id排序 DB::table('tablename')->where('id','!=',2)->get(); 不等于2 DB::table('tablename')->where('id','!=',2)->where('id','>',5)->get(); 可以使用多個(gè)where DB::table('tablename')->where('id','!=',2)->OrWhere('id','>',5)->get(); 或者 DB::table('tablename')->whereBetween('id',[2,5])->get(); 閉包之間 DB::table('tablename')->whereIn('id',[2,5,9])->get(); DB::table('tablename')->whereNotIn('id',[2,5,9])->get(); DB::table('tablename')->whereNull('id')->get(); 為空的話(huà)就可以查詢(xún)出來(lái) DB::table('tablename')->take(3)->get(); 只查詢(xún)3個(gè) DB::table('tablename')->limit(3)->get(); 只查詢(xún)3個(gè) DB::table('tablename')->skip(2)->take(3)->get(); 只查詢(xún)3個(gè)跳過(guò)第二個(gè) DB::table('tablename')->where('id','!=',2)->pluck('title'); 只返回它的title DB::table('tablename')->count(); 有多少條記錄 DB::table('tablename')->max('id'); DB::table('tablename')->min('id'); DB::table('tablename')->avg('id'); DB::table('tablename')->sum('id');
多表關(guān)聯(lián)
在Post中定義
public function comment(){ return $this->hasMany('Comment','post_id') } 正向關(guān)聯(lián) 一對(duì)多 一對(duì)一是hasOne
在Comment中定義
public function post(){ return $this->belongsTo('Post','post_id') } 反向關(guān)聯(lián)
取得關(guān)聯(lián)值
Post::find(2)->comment 就可以得到Comment這張表的內(nèi)容 //這樣查詢(xún)一個(gè)是可以的 查詢(xún)多個(gè)就要設(shè)置預(yù)載入 查詢(xún)多個(gè) Post::with('comment')->get(); Post::with(['comment'=>function($query){$query->where('id','>',2)}])->get(); 加條件
感謝大家的閱讀,希望大家有所收益。
本文轉(zhuǎn)自:https://blog.csdn.net/Happy_CSDN/article/details/49363219
推薦教程:《php教程》
The above is the detailed content of How to learn laravel framework in php (novice beginner). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Single-page applications (SPAs) can be built using Laravel and Vue.js. 1) Define API routing and controller in Laravel to process data logic. 2) Create a componentized front-end in Vue.js to realize user interface and data interaction. 3) Configure CORS and use axios for data interaction. 4) Use VueRouter to implement routing management and improve user experience.

Efficient methods for testing Laravel API interfaces include: 1) using Laravel's own testing framework and third-party tools such as Postman or Insomnia; 2) writing unit tests, functional tests and integration tests; 3) emulating a real request environment and managing database status. Through these steps, the stability and functional integrity of the API can be ensured.

Custom Laravel user authentication logic can be implemented through the following steps: 1. Add additional verification conditions when logging in, such as mailbox verification. 2. Create a custom Guard class and expand the authentication process. Custom authentication logic requires a deep understanding of Laravel's authentication system and pay attention to security, performance and maintenance.

The steps to create a package in Laravel include: 1) Understanding the advantages of packages, such as modularity and reuse; 2) following Laravel naming and structural specifications; 3) creating a service provider using artisan command; 4) publishing configuration files correctly; 5) managing version control and publishing to Packagist; 6) performing rigorous testing; 7) writing detailed documentation; 8) ensuring compatibility with different Laravel versions.

Integrating social media login in the Laravel framework can be achieved by using the LaravelSocialite package. 1. Install the Socialite package: use composerrequirelaravel/socialite. 2. Configure the service provider and alias: add relevant configuration in config/app.php. 3. Set API credentials: Configure social media API credentials in .env and config/services.php. 4. Write controller method: Add redirection and callback methods to handle social media login process. 5. Handle FAQs: Ensure user uniqueness, data synchronization, security and error handling. 6. Optimization practice:

Implementing password reset function in Laravel requires the following steps: 1. Configure the email service and set relevant parameters in the .env file; 2. Define password reset routes in routes/web.php; 3. Customize email templates; 4. Pay attention to email sending problems and the validity period of tokens, and adjust the configuration if necessary; 5. Consider security to prevent brute-force attacks; 6. After the password reset is successful, force the user to log out of other devices.

Common security threats in Laravel applications include SQL injection, cross-site scripting attacks (XSS), cross-site request forgery (CSRF), and file upload vulnerabilities. Protection measures include: 1. Use EloquentORM and QueryBuilder for parameterized queries to avoid SQL injection. 2. Verify and filter user input to ensure the security of output and prevent XSS attacks. 3. Set CSRF tokens in forms and AJAX requests to protect the application from CSRF attacks. 4. Strictly verify and process file uploads to ensure file security. 5. Regular code audits and security tests are carried out to discover and fix potential security vulnerabilities.

Middleware is a filtering mechanism in Laravel that is used to intercept and process HTTP requests. Use steps: 1. Create middleware: Use the command "phpartisanmake:middlewareCheckRole". 2. Define processing logic: Write specific logic in the generated file. 3. Register middleware: Add middleware in Kernel.php. 4. Use middleware: Apply middleware in routing definition.
