Laravel

Aus Vokabulabor
Zur Navigation springen Zur Suche springen

Links

Projekt einrichten

PROJ=langutor
PASSW=topsecret
BASE=/home/ws/php/$PROJ
cd $(basename $BASE)
composer create-project laravel/laravel $PROJ
cd $BASE
composer require laravel/ui
composer require spatie/laravel-permission
#composer require blade-ui-kit/blade-icons
php artisan ui bootstrap --auth
dbtool create-db-and-user lrv$PROJ $PROJ "$PASSW"
sed -i -e "s/DB_DATABASE=.*/DB_DATABASE=lrv$PROJ/" \
  -e "s/DB_USERNAME=.*/DB_USERNAME=$PROJ/" \
  -e "s/DB_PASSWORD=.*/DB_PASSWORD=$PASSW/" .env

M_HOST=mail.gmx.net
M_PORT=587
M_USER=example@gmx.de
M_PW=Top.Secret42
sed -i -e "s/MAIL_MAILER=.*/MAIL_MAILER=smtp/" \
  -e "s/MAIL_HOST=.*/MAIL_HOST=$M_HOST/" \
  -e "s/MAIL_PORT=.*/MAIL_PORT=$M_PORT/" \
  -e "s/MAIL_USERNAME=.*/MAIL_USERNAME=$M_USER/" \
  -e "s/MAIL_PASSWORD=.*/MAIL_PASSWORD=$M_PW/" \
  -e "s/MAIL_ENCRYPTION=.*/MAIL_ENCRYPTION=STARTTLS/" \
  -e "s/MAIL_FROM_ADDRESS=.*/MAIL_FROM_ADDRESS=\"$M_USER\"/" .env

Bootstrap 5

  • app\Providers\AppServiceProvider.php
use Illuminate\Pagination\Paginator;
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Paginator::useBootstrapFive();
}

Projekt aktualisieren

npm install
npm run dev

Deployment (Produktivversion)

Update mittels Git

  • Basisverzeichnis sei BASE=/srv/www/taskx
PROJ=taskx
BASE=/srv/www/$PROJ
REPO=/home/git/repo/$PROJ.git
cd $BASE
git clone $REPO
ln -s $PROJ/public public
# composer cache:
chown -R www-data:www-data /var/www
  • Kopieren der Datei .env vom Entwicklungsserver und anpassen
APP_URL=...
APP_KEY=
APP_DEBUG=false
APP_ENV=production
LOG_LEVEL=error # debug, info, notice, warning, error, critical, alert, emergency.
DB_HOST=...
DB_DATABASE=...
DB_USERNAME=...
DB_PASSWORD=...
MAIL_...
php artisan key:generate

Update-Prozess

sudo -u www-data -s
composer update --optimize-autoloader --no-dev
php artisan migrate
php artisan db:seed

php artisan config:cache
php artisan event:cache
php artisan route:cache
php artisan view:cache
php artisan optimize
php artisan storage:link
npm install
npm run build

Artisan Kommandos

make:controller

php artisan make:controller RoleController --invokable
  • app/Http/Controllers/RoleController
class RoleController extends Controller{
    public function __invoke(Request $request){}
}
  • web.php:
Route::post('/role', RoleController::class);

php artisan make:controller RoleController --resource 
Erzeugt eine Date
  • app/Http/Controllers/RoleController
class RoleController extends Controller{
  public function index(){}
  public function create(){}
  public function store(Request $request){}
  public function show(string $id){}
  public function edit(string $id){}
  public function update(Request $request, string $id){}
  public function destroy(string $id){}
}

make:view

php artisan make:view role.index 
  • resources/views/role/index.blade.php
<div>
    <!-- ... -->
</div>

Sonstige

php artisan make:seeder PermissionSeeder
php artisan make:migration create_${TABLE}_table
php artisan migrate:fresh --seed
php artisan make:controller RoleController --resource
php artisan make:request StoreRoleRequest
php artisan make:view users.index

Integrierter Webserver

php artisan serve

I18N

  • config/app.php
'locale' => 'de_DE',
'available_locales' => [
  'English' => 'en',
  'German' => 'de_DE',
],
  • resources/lang/de_DE.json
{
  "Cancel": "Abbrechen",
  "OK": "OK",
  "Save": "Speichern"
}