Inspired by Laravel udemy course
-
- Eloquent - ORM, Scope query
- Eloquent - relationship
- Database - Tinker - use Php Storm database (support for Ultimate version only) or seeders instead
- Forms and validation - package installation LaravelCollective - Date - Mutators - Accessors - Uploading files
- Form login - Middleware - Laravel Session
- Email - Application - Dev
Reference:
-
https://viblo.asia/p/laravel-beauty-recipes-best-practices-6BAMYk9Evnjz
-
More good articles
-
Model: database objects
-
View: html
-
Controller: Middle-Man
-
Lavarel 5.5 requires PHP 7+
-
XAMPP or Lavarel Homestead or Laragon ?
-
XAMPP: enable htdocs folder visible: Volumes -> Mount
alias htdocs="cd $USER/.bitnami/stackman/machines/xampp/volumes/root/htdocs"
-
Lavarel Homestead tutorial - link
-
-
Packagist: php package repository - like java arifactory
-
Composer: php package manager
-
Installation: https://getcomposer.org/download/
-
add into zshrc/bashrc:
export PATH="$PATH:$HOME/.composer/vendor/bin"
-
-
Grant global access
mv ~/.composer.phar /usr/local/bin/composer chmod u+x /usr/local/bin/composer
-
htdocs
folder: Create a project-
by composer
composer create-project --prefer-dist laravel/laravel firstapp 5.5.28
-
by laravel installer
composer global require "laravel/installer" lavarel new firstapp
-
grant access for storage folder after created
chmod -R o+wr firstapp/storage
-
-
show versions laravel/laravel from repository
composer info -a laravel/laravel
-
-
Virtual Host: enter the website by
laravelfirstapp.dev
instead oflocalhost/laravel-firstapp/public
-
xamppfiles/etc/httpd.conf: enable
httpd-vhosts.conf
# Virtual hosts Include etc/extra/httpd-vhosts.conf
-
xamppfiles/etc/extra/httpd-vhost.conf
replace the content with
# localhost <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs" ServerName localhost ServerAlias www.localhost </VirtualHost> # laravelfirstapp.dev <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/laravel-firstapp/public" ServerName laravelfirstapp.dev </VirtualHost>
-
vim /etc/hosts
127.0.0.1 laravelfistapp.dev
-
-
How to fix common errors ?
-
Unable to access phpmyadmin ? Enable access phpmyadmin
# etc/extra/httpd-xampp.conf <Directory "/opt/lampp/phpmyadmin"> AllowOverride AuthConfig Limit Require all granted Order allow,deny Allow from all # Require local # ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var </Directory>
-
Your connection is private on Chrome ?
use FireFox - or create host name without ending with
dev
-
Got forbidden access ?
- sure that this is not enable
# Virtual hosts #Include etc/extra/httpd-vhosts.conf
-
return $var; //json output
var_dumps($var);
dd($var);
dump($var);
-
config/app.php:
- configuration: app name, env, locale, debug,...
- import providers - 3rd apps
-
config/database.php: database connection configuration
-
.env
: properties file, get value by functionenv($key, $default = null)
-
vendor: dependencies installation folder
-
app: events, exceptions, http, ...
-
artisan: show available commands
php artisan
-
routes/web.php
-
Routes folder:
view
function mapping to resources/views/**.blade.php- e.g: route with path variables
Route::get('/students/{id}/{name}', function ($id, $name) { return "Hello student with id: ". $id ." - name: ". $name; });
-
List all routes:
php artisan route:list
-
Naming a route
Route::get('/teachers', array('as' => 'admin.home', function () { $url = route('admin.home'); return "your url: " . $url; })); Route::get('/post/{id}', ['as' => 'home.post', 'uses' => 'AdminPostsController@post']); // if you dont want use default generated urls Route::resource('/admin/users', 'AdminPostsController@post', ['names' => [ 'index' => 'your.custom.route' 'store' => 'your.custom.store' 'create' => 'your.custom.route.create' 'edit' => 'your.custom.route.edit' 'update' => 'your.custom.route.update' 'delete' => 'your.custom.route.delete' ]])
-
Create a controller:
-
Normal controller
php artisan make:controller StudentsController
-
Controller for show, update, edit, store,... :
php artisan make:controller --resource StudentsController
-
-
Mapping a route to controller method
Route::get('/student', 'StudentsController@index');
-
Passing & retriving data
Route::get('/student/{id}', 'StudentsController@index'); .... class StudentsController { public function index($id) { } }
-
Resource route: generate full REST-API: GET-index, GET-create, GET-show, GET-edit, POST, PUT/PATH, DELETE methods by just a single line of code.
Route::resource('/student', 'StudentsController');
-
Blade file: template engine
-
Create a custom view and custom method
- Add a method in the controller
- Add a route for that method in
routes/web.php
- Create blade php file in
resources/views
-
Passing data to views
-
web.php
Route::get('/contact/{name}', 'StudentsController@contactStudent');
-
Controller
public function contactStudent($name) { return view('pages.contact-student')->with('name', $name); # or # return view('pages.contact-student', compact('name')); }
-
View
<h1>Hello {{$name}}</h1>
-
- create layouts/app.blade.php: @yield('content'), @yield('footer')
- using:
- @extends('layouts.app')
- @section('content') ... @endsection
@if(count($people))
<ul>
@foreach($people as $person)
<li>{{$person}}</li>
@endforeach
</ul>
@endif
composer install && composer update;
chmod -R o+rw storage;
composer run post-root-package-install;
composer run post-create-project-cmd;
npm install;
npm run dev;
// refreshing cache
php artisan clear-compiled
composer dump-autoload
php artisan optimize
php artisan down
php artisan up
Install xdebug or http://phpdebugbar.com/ or https://tracy.nette.org/en/
https://dev.to/david_j_eddy/5-php-tools-to-make-your-life-more-enjoyable-1jl1
PHPStorm - Jetbrains