General Laravel Development Information
See PHP General for general PHP development information.
Dependency Injection
- Where possible use dependency injection to resolve classes
- When that is not possible, use the
app()helper function
Example
class UserController
{
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
public function index()
{
return $this->userRepository->all();
}
}
class ConvertFeetToInches
{
public function execute($length, $measurement = Measurement::INCHES)
{
if ($measurement === Measurement::METERS) {
$length = app(ConvertMetersToFeet::class)->execute($length);
}
return $feet * 12;
}
}
Facades and Helpers
- Where possible use helper functions
- When that is not possible, use Facades
- Use the shorter helper functions over the chained methods
Example
session('cart');
// vs
$request->session()->get('cart');
Helpers vs PHP Functions
- Use Laravel helper functions over PHP functions
Example
// Laravel helper function
Str::position('This is my name', 'my');
// over PHP function
strpos('This is my name', 'my');