Laravel: Difference between revisions

 
(9 intermediate revisions by the same user not shown)
Line 2: Line 2:
One of the best parts of Laravel is their excellent video course on web development.
One of the best parts of Laravel is their excellent video course on web development.
The course is published on Laracasts.
The course is published on Laracasts.
Why use Laravel?
* Security - [https://auth0.com/blog/why-laravel-is-the-recommended-framework-for-secure-mission-critical-applications/ auth0 blog]
* Authentication - [https://laravel.com/docs/master/authentication Authentication Docs]
* Eloquent ORM (Object relational mapping) - [https://laravel.com/docs/master/eloquent Eloquent Docs]


==Usage==
==Usage==
Line 41: Line 36:


==Blade==
==Blade==
===Components (7.x)===
===Components===
:Note: Components were completely revamped in Laravel 7. If you're using Laravel 6 (LTS), see Components (<= 6.x).   
: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].   
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].   


Line 53: Line 48:
php artisan make:component Alert
php artisan make:component Alert
</pre>
</pre>
===Components (<= 6.x)===


==Controllers==
==Controllers==
Line 119: 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==