Please enable JavaScript.
Coggle requires JavaScript to display documents.
Laravel (ROUTING (routes / web (api.php for REST), Passing data to view
…
Laravel
-
ROUTING
-
Passing data to view
-
compact('varname')
-
with('varname'[, 'value'])
-
withVarname
-
return view("bladeFile", [associate array]);
-
return view('folder.file | folder/file)
-
Prefixing
Route::group(['prefix' => 'users'], function() {
Route::get()...
});
Naming
Route::(...)->name('name-of-route');
Examples: redirect()->named-route();
redirect()->route('route-name');
-
BLADE
-
include some partial into the layout
-
yield placeholder of section
-
-
-
MODEL
-
-
pay attention to terminology, make near to real life (addComment, publish)
-
-
-
SECURITY
-
-
Validation
HTML5 validation tools: type
, required
in Controller method
$this->validate(request(), [rules])
-
-
-
-
MIDDLEWARE
$this->middleware('auth', ['only|except' => methodName])
FILTERING
Request
-
if (request('query'))
- php may instantiate var inside the clause
-
$posts->whereMonth/Year('columnName', value)
-
-
Filtering as a query scope
- method -
scopeSmth($query, $filters array)
-
if (! count($filters)) return;
-
VIEW COMPOSER
- Define which component is needed repeatedly
- Make it a partial
- Make a static function that gives the required variable
- in
AppServiceProvider@boot
view()->composer('partial.name', function($view) {
$view->with('varname', \App\Class::staticFn());
});
-
DI, SERVICE PROVIDERS/CONTAINERS
Dependency injection - adding arguments
Laravel will do this automatically if it can handle it (russian doll like)
Service container
-
Registering a class in Service Container
- in
register
method of AppServiceProvider
-
$this->app->bind|singleton('Classname', callback);
-
App::bind('App\Smth\Anthing', function() { return \App\Smth\Anthing(needed args); }
-
App::singleton()
Resolving things
-
App::make(Classpath)
-
resolve(Classpath)
-
app(Classpath)
-
MAILING :email:
- Configure ENV variables and restart the server
- In the dedicated method add
\Mail::to($user)->send(new EmailLayout($dataToBeAddedToTheView));
- Make email class:
php artisan make:mail EmailLayoutName
- add public $users prop to make it available in the view OR view()->with('var', 'value') :
- Build email layout like a blade layout
-
FORM REQUEST
-
authorize
- can a given user use this method? If auth logic is done elsewhere, just return true
-
Form Object
persist
method - logic of saving stuff and things should be done after it.
Smth::create(request([1, 2 ,3]))-> $this->only([1, 2, 3]);
RegForm $form, inside contrmethod: $form->persist()
-
SESSION AND FLASH MSGS
session() -> instance
session('msg', 'default msg');
session(['msg' => 'value'])
Flashing
session->flash('msg', 'value')
EVENTING
-
-
Shortcut way
- Register in EventServiceProvider
-
event:generate
-
-
-