快取指令
laravel官方文件
安裝laravel
php & composer
sudo apt install php composer php-cli php-common php-mbstring php-xml php-zip php-mysql php-json php-bcmath php-curl php-gd php-tokenizer php8.1-xml php-fpm mariadb-server nginx
這裡要是需要或習慣安裝apache or nginx,nginx需要使用php-fpm
php -v
sudo apt install curl php-cli php-mbstring git unzip
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
composer --version
install laravel
composer create-project --prefer-dist laravel/laravel your-project-name
cd your-project-name
install apache2 or nginx
apache
sudo vi /etc/apache2/sites-available/your-project-name.conf
<VirtualHost *:80>
ServerName your-domain-or-ip
DocumentRoot /var/www/html/your-project-name/public
<Directory /var/www/html/your-project-name>
AllowOverride All
</Directory>
</VirtualHost>
sudo a2enmod rewrite
Enable the virtual host:
sudo a2ensite your-project-name.conf
Restart Apache for the changes to take effect:
sudo systemctl restart apache2
nginx
sudo vi /etc/nginx/sites-available/your-project-name
server {
listen 80 default_server;
server_name your-domain-or-ip;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
server_name your-domain-or-ip;
include snippets/ssl;
include snippets/ssl;
root /var/www/html/your-project-name/public;
index index.php;
location / {
# try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php?$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
add_header X-XSS-Protection "1; mode=block";
# add_header 'Access-Control-Allow-Origin' *;
# add_header 'Access-Control-Allow-Credentials' true;
# add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
# add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
location ~* \.(jpg|jpeg|gif|png|svg)$ {
expires 365d;
}
location ~* \.(pdf|css|html|js|swf)$ {
expires 2d;
}
location ~ /\.ht {
deny all;
}
location ~ ^/\.user\.ini {
deny all;
}
location ~ /.well-known/acme-challenge {
root /var/www/html;
allow all;
}
location ~ /\.ht {
deny all;
}
}
Replace your-domain-or-ip with your actual domain name or server IP address.
Enable the Nginx server block:
sudo ln -s /etc/nginx/sites-available/your-project-name /etc/nginx/sites-enabled/
Test the Nginx configuration for any syntax errors:
sudo nginx -t
Restart Nginx for the changes to take effect:
sudo systemctl restart nginx
MariaDB 資料庫
在安裝好 MariaDB 資料庫後,使用前建議先調整一下安全性的設定:
MariaDB/MySQL 資料庫安全性設定
sudo mysql_secure_installation
使用 root 登入 MariaDB/MySQL 資料庫
sudo mysql -u root -p
建立 laravel 資料庫
CREATE DATABASE laravel;
CREATE USER `user`@`localhost` IDENTIFIED BY 'yourpassword';
GRANT ALL ON laravel.* TO `user`@`localhost`;
FLUSH PRIVILEGES;
編輯 Laravel 專案目錄中的 .env 設定檔,將資料庫的相關資訊填入其中:
DB_DATABASE=laravel
DB_USERNAME=user
DB_PASSWORD=yourpassword
除了 .env 的設定檔之外,也可以直接編輯 config/database.php 中的資料庫設定。
php artisan serve 伺服器 #開啟服務,預設8000
php artisan migrate #建立使用資料庫
php artisan about #快速瀏覽環境設定
Configure Laravel
cp .env.example .env
Generate a new application key
php artisan key:generate
Set the appropriate permissions on Laravel directories
sudo chown -R www-data:www-data /var/www/html/your-project-name/storage
sudo chmod -R 775 /var/www/html/your-project-name/storage
Laravel Breeze
composer require laravel/breeze --dev # 安裝Breeze
安裝npm跟nodejs,請注意版本問題
sudo apt install npm
sudo apt install nodejs
ubuntu 系統函式庫的nodejs版本通常會太舊,可以用下方方式處理,請問注意版本 ,或可以從https://nodejs.org/en下載
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
php artisan breeze:install
php artisan migrate
npm install
直接運作
npm run dev
php artisan用法
php artisan env:encrypt #加密環境變數
備註:完成後會加密.env並更名成.env.encrypted,然後會輸出密碼,也可以用下方指令修改密碼。
php artisan env:encrypt --key=密碼
php artisan env:decrypt #解密
備註:--key 解密密碼
php artisan env:decrypt --force
php artisan serve #不使用webagent直接運作,預設8000port
php artisan serve #不使用webagent直接運作,預設8000port
製作mode(database一起建立)
資料庫資料的建立與使用概略流程:
make:model->migrate->make:factory->make:seeder->db:seed->make:controller->create view->set route
php artisan make:model Movie -m
php artisan migrate #資料庫創建
php artisan make:migration create_新table名稱_table #建立新table
php artisan make:migration create_aboutme_photo — table=aboutme
php artisan migrate:status #資料庫建制狀態
php artisan migrate:rollback #資料庫還原
php artisan migrate:refresh #資料庫重鍵(資料會不見)
製作Factory資料夾
php artisan make:factory MovieFactory --model=Movie
製作seeder
php artisan make:seeder MovieSeeder
製作control
php artisan make:controller MovieController
啟用維護模式
php artisan down
備註:
後方可以增加--refresh=(秒數) 來告訴伺服器多久要重新整理一次
--render="errors::503" 可以放置503網頁的view
關閉維護模式
php artisan up
使用密鑰繞過維護模式
php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
使用方式:https://example.com/1630542a-246b-4b66-afa1-dd72a4c43515
在app新增job類別
php artisan make:job
備註: 可使用php artisan make list 來檢查可以新增的類別
route列表
php artisan route:list
備註:
- --except-vendor是會顯示影藏在第三方的route
- --only-vendor 只顯示第三方套件
- -v 會顯示Route Middleware
- --path=api cjo4vu04g4eo32u/4
- 會顯示給URI開頭的Route
php artisan vendor:publish --tag=laravel-pagination
前台使用tailwindcss 而後台是 bootstrap
這個路徑就是 Laravel 內置的格式/排版 resources/views/vendor/pagination
清除cache
php artisan cache:clear
php artisan route:clear
php artisan view:clear
Route使用
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::match(['get', 'post'], '/', function () {
// ...
});
Route::any('/', function () {
// ...
});
重新導向
Route::redirect('/here', '/there');
Route::redirect('/here', '/there', 301);
Route::permanentRedirect('/here', '/there');
重導至路徑為 users/index 的位址
redirect()->to("users/index");
使用全域輔助捷徑,有同樣的效果
redirect("users/index");
使用靜態介面, 也有同樣的效果
Redirect::to("users/index");
靜態介面捷徑
Redirect("users/index");
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
to()
function to($to = null, $status = 302, $header = [], $secure = null)
$to : 有效的內部路徑
$status 是 HTTP 狀態
$header 是需要一併傳送的 HTTP 標頭
$secure : http vs. https 的預設選項
route()
function route($to = null, $parameters =[], $status = 302, $headers = [])
$to : 特定路由名稱
$parameters : 路由所需的參數
withInput()
把表單輸入的資料傳給目標頁面
redirect("users.edit")
->withInput()
->with(["success" => "update success", "id" => 2]);