關(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è)面,如果在本地的話那就是localhost/laravelproject/public,就可以運(yùn)行。
接下來(lái)介紹下laravel目錄結(jié)構(gòu),首先介紹下public的index.php文件 里面主要是加載了開(kāi)始文件然后才能成功運(yùn)行l(wèi)aravel,具體的兩個(gè)文件你可以在根目錄下bootstrap文件夾中找到。現(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就可以訪問(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(); 為空的話就可以查詢(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

When choosing a suitable PHP framework, you need to consider comprehensively according to project needs: Laravel is suitable for rapid development and provides EloquentORM and Blade template engines, which are convenient for database operation and dynamic form rendering; Symfony is more flexible and suitable for complex systems; CodeIgniter is lightweight and suitable for simple applications with high performance requirements. 2. To ensure the accuracy of AI models, we need to start with high-quality data training, reasonable selection of evaluation indicators (such as accuracy, recall, F1 value), regular performance evaluation and model tuning, and ensure code quality through unit testing and integration testing, while continuously monitoring the input data to prevent data drift. 3. Many measures are required to protect user privacy: encrypt and store sensitive data (such as AES

There are three main ways to set environment variables in PHP: 1. Global configuration through php.ini; 2. Passed through a web server (such as SetEnv of Apache or fastcgi_param of Nginx); 3. Use putenv() function in PHP scripts. Among them, php.ini is suitable for global and infrequently changing configurations, web server configuration is suitable for scenarios that need to be isolated, and putenv() is suitable for temporary variables. Persistence policies include configuration files (such as php.ini or web server configuration), .env files are loaded with dotenv library, and dynamic injection of variables in CI/CD processes. Security management sensitive information should be avoided hard-coded, and it is recommended to use.en

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction

To build a PHP content payment platform, it is necessary to build a user management, content management, payment and permission control system. First, establish a user authentication system and use JWT to achieve lightweight authentication; second, design the backend management interface and database fields to manage paid content; third, integrate Alipay or WeChat payment and ensure process security; fourth, control user access rights through session or cookies. Choosing the Laravel framework can improve development efficiency, use watermarks and user management to prevent content theft, optimize performance requires coordinated improvement of code, database, cache and server configuration, and clear policies must be formulated and malicious behaviors must be prevented.

Laravel's EloquentScopes is a tool that encapsulates common query logic, divided into local scope and global scope. 1. The local scope is defined with a method starting with scope and needs to be called explicitly, such as Post::published(); 2. The global scope is automatically applied to all queries, often used for soft deletion or multi-tenant systems, and the Scope interface needs to be implemented and registered in the model; 3. The scope can be equipped with parameters, such as filtering articles by year or month, and corresponding parameters are passed in when calling; 4. Pay attention to naming specifications, chain calls, temporary disabling and combination expansion when using to improve code clarity and reusability.

The core idea of PHP combining AI for video content analysis is to let PHP serve as the backend "glue", first upload video to cloud storage, and then call AI services (such as Google CloudVideoAI, etc.) for asynchronous analysis; 2. PHP parses the JSON results, extract people, objects, scenes, voice and other information to generate intelligent tags and store them in the database; 3. The advantage is to use PHP's mature web ecosystem to quickly integrate AI capabilities, which is suitable for projects with existing PHP systems to efficiently implement; 4. Common challenges include large file processing (directly transmitted to cloud storage with pre-signed URLs), asynchronous tasks (introducing message queues), cost control (on-demand analysis, budget monitoring) and result optimization (label standardization); 5. Smart tags significantly improve visual

User permission management is the core mechanism for realizing product monetization in PHP development. It separates users, roles and permissions through a role-based access control (RBAC) model to achieve flexible permission allocation and management. The specific steps include: 1. Design three tables of users, roles, and permissions and two intermediate tables of user_roles and role_permissions; 2. Implement permission checking methods in the code such as $user->can('edit_post'); 3. Use cache to improve performance; 4. Use permission control to realize product function layering and differentiated services, thereby supporting membership system and pricing strategies; 5. Avoid the permission granularity is too coarse or too fine, and use "investment"

Laravel's configuration cache improves performance by merging all configuration files into a single cache file. Enabling configuration cache in a production environment can reduce I/O operations and file parsing on each request, thereby speeding up configuration loading; 1. It should be enabled when the application is deployed, the configuration is stable and no frequent changes are required; 2. After enabling, modify the configuration, you need to re-run phpartisanconfig:cache to take effect; 3. Avoid using dynamic logic or closures that depend on runtime conditions in the configuration file; 4. When troubleshooting problems, you should first clear the cache, check the .env variables and re-cache.
