Laravel: Difference between revisions
No edit summary |
|||
| (17 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
[https://laravel.com Laravel] is a popular MVC framework for PHP.<br> | |||
[https://laravel.com | One of the best parts of Laravel is their excellent video course on web development. | ||
The course is published on Laracasts. | |||
==Usage== | ==Usage== | ||
| Line 37: | Line 34: | ||
npm run dev | npm run dev | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Blade== | |||
===Components=== | |||
:Note: Components were completely revamped in Laravel 7. | |||
If you don't know anything watch [https://laracasts.com/series/blade-component-cookbook Blade Component Cookbook] or read the [https://laravel.com/docs/7.x/blade Blade Templates]. | |||
Components in Laravel 7 have two parts: | |||
* A php script in <code>app/View/Components</code>. E.g. <code>MyComponent.php</code> | |||
* A blade template in <code>resources/views/components</code>. E.g. <code>my-component.blade.php</code> | |||
To get started, run: | |||
<pre> | |||
php artisan make:component Alert | |||
</pre> | |||
==Controllers== | ==Controllers== | ||
| Line 59: | Line 70: | ||
Route::options($uri, $callback); | Route::options($uri, $callback); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Theses can also be simplified with a single <code>Route::resource($uri, $className);</code> for a [https://laravel.com/docs/7.x/controllers resource controller] | |||
===Parameters/Variables=== | ===Parameters/Variables=== | ||
| Line 65: | Line 78: | ||
return 'User '.$id; | return 'User '.$id; | ||
}); | }); | ||
</syntaxhighlight> | |||
==Database== | |||
===Query Builder=== | |||
;From subquery: | |||
<syntaxhighlight lang="php"> | |||
$subquery = DB::connection('bayarea')->table(DB::raw("restaurants x, parking y")) | |||
->select("x.id as id1", "y.id as id2")->where("x.id", "=", $restaurant_id); | |||
$mainquery = DB::connection('bayarea')->table(DB::raw("({$subquery->toSql()}) as t1"))->mergeBindings($subquery)->...->get(); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
==Laravel Mix== | ==Laravel Mix== | ||
Laravel mix is an asset compiler/minifier for Laravel | Laravel mix is an asset compiler/minifier for Laravel which uses webpack.<br> | ||
Your configuration will be in <code>webpack.mix.js</code><br> | Your configuration will be in <code>webpack.mix.js</code><br> | ||
The following may be convenient: | The following may be convenient: | ||
<syntaxhighlight lang="js"> | <syntaxhighlight lang="js"> | ||
const fs = require('fs-extra'); | const fs = require('fs-extra'); | ||
class Rm { | |||
name() { | |||
return "rm"; | |||
} | |||
register(to) { | |||
fs.removeSync(to); | |||
} | |||
} | } | ||
mix.extend('rm', new Rm()); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 82: | Line 112: | ||
Variables can be passed from PHP to JS using view replacement. Note that while your JS is typically separate from your view, you will need to include some JS in your view to use this feature.<br> | Variables can be passed from PHP to JS using view replacement. Note that while your JS is typically separate from your view, you will need to include some JS in your view to use this feature.<br> | ||
[https://stackoverflow.com/questions/30074107/laravel-5-passing-variable-to-javascript/46805484 Example]. | [https://stackoverflow.com/questions/30074107/laravel-5-passing-variable-to-javascript/46805484 Example]. | ||
==Deployment== | |||
===Directly=== | |||
Point your webserver with PHP to the public folder.<br> | |||
If your application is publically accessible, make sure APP_DEBUG is set to false. | |||
===Docker=== | |||
See the example Dockerfile below: | |||
<syntaxhighlight lang="Dockerfile"> | |||
# Dockerfile | |||
FROM php:8.1-apache | |||
RUN apt-get update && \ | |||
apt-get install -y nodejs npm zip libpq-dev sudo && \ | |||
a2enmod rewrite | |||
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer | |||
ENV APACHE_DOCUMENT_ROOT /var/www/html/public | |||
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf | |||
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf | |||
RUN sed -i 's#AllowOverride [Nn]one#AllowOverride All#' /etc/apache2/apache2.conf | |||
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" | |||
# RUN sed -i 's#upload_max_filesize = 2M#upload_max_filesize = 200M#' "$PHP_INI_DIR/php.ini" | |||
# RUN sed -i 's#post_max_size = 8M#post_max_size = 200M#' "$PHP_INI_DIR/php.ini" | |||
RUN chown www-data:www-data /var/www -R | |||
ADD package.json package-lock.json /var/www/html/ | |||
RUN sudo -u www-data npm ci | |||
ADD composer.json composer.lock artisan /var/www/html/ | |||
ADD app /var/www/html/app | |||
ADD config /var/www/html/config | |||
ADD database /var/www/html/database | |||
ADD bootstrap /var/www/html/bootstrap | |||
ADD routes /var/www/html/routes | |||
RUN composer install | |||
# Add remaining files | |||
ADD . /var/www/html/ | |||
RUN cp .env.prod.example .env | |||
RUN chmod 775 /root && \ | |||
chown www-data:www-data -R /var/www/html && \ | |||
npm run prod | |||
</syntaxhighlight> | |||
==Security== | |||
===Vulnerabilities=== | |||
* [https://nvd.nist.gov/vuln/detail/CVE-2021-3129 CVE-2021-3129] - In Laravel < 8.4.2, leaving debug mode on can lead to remote execution. | |||
==Resources== | ==Resources== | ||
* [https://laravel.com/docs/ Laravel Documentation] | * [https://laravel.com/docs/ Laravel Documentation] | ||
* [https://laracasts.com/series/laravel-from-scratch-2018 Laravel from Scratch | * [https://laracasts.com/series/laravel-from-scratch-2018 Laravel 5.7 From Scratch by Jeffrey Way] | ||
* [https://laracasts.com/series/laravel-6-from-scratch Laravel 6 From Scratch by Jeffrey Way] | |||
* [https://laracasts.com/series/blade-component-cookbook Blade Component Cookbook] | |||