- When talking about ways of coding, choose and follow one!
- Research before you code. Also, make a checklist of test cases before you begin working on your task.
- Stop being lazy! Avoid copy paste (unless copying variable names)
- It's your code! Don't let anyone say "Who in the world wrote this?" about it.
- Fix it! rather than working around it...
- Take responsibility and ownership!
- Don't be afraid to learn! Knowledge is Power!
- Check your corners & stay in your lane. Don't touch others code unless you are assigned it! If you still do, memorize point #6!
- Don't be afraid to ask. But have the courtesy to research on it before going to anyone.
- Have a beer! Or probably Coconut water... or whatever you like... Learn to Relax.
And remember, git blame tells us everything. (Read point #10 if this line scares you!... LOL)
- Atom - Atom Beautify - Atom Clock - Blade Snippets - Busy Signal - DocBlockr - Goto-Definition - Language-Blade - Linter-PHP - Pigments - Prettier Atom
- VSCode - Laravel Blade Snippets
Laravel follows the PSR-2 coding standard and the PSR-4 autoloading standard.
- Helper Functions - snake_case()
- Class Functions - camelCase()
- Variable Names - camelCase everywhere.
- Migrations - snake_case
- Create Table -
create_users_table
- Alter Table or Change Columns -
alter_users_table_add_date_of_birth_field
(Singular) oralter_users_table_add_profile_fields
(Plural)
- Create Table -
- Artisan Commands - kebab-case.
php artisan user:expire-bookings
- Route Names - kebab-case
Route::get('about-us', 'PagesController@aboutUs')
- Controller Names - PascalCase (Plural Model Name, For eg.
UsersController
) - Use complete words for any & everything. For eg.
userRepository
instead ofuserRepo
.
- MySQL - Charset -
utf8mb4
- Collation -utf8mb4_unicode_ci
- Timezone - UTC
- Drivers - Log -
stack
- Mail -smtp
- Cache -redis
- Queue -redis
- Session -redis
- Model Namespace -
App\Models
- Resource Namespace -
resources/assets
(Use Webpack to move to public) - Storage permissions (this SO Answer is best)
$ cd /path/to/your/laravel/root/directory $ sudo chown -R $(whoami):_www storage bootstrap/cache $ sudo chmod ug+rwx storage bootstrap/cache $ sudo find storage bootstap/cache -type f -exec chmod 664 {} \; $ sudo find storage bootstrap/cache -type d -exec chmod 775 {} \;
config/auth.php
-users
provider model should beApp\Models\User::class
- .gitignore
/node_modules /public/css /public/js /public/hot /public/storage /storage/*.key /vendor /.idea /.vscode /.vagrant Homestead.json Homestead.yaml npm-debug.log yarn-error.log .env .env.testing
- Declare class variables & add comments to them.
/**
* City Repository
*
* @var \App\Repositories\Contract\CityRepositoryInterface
*/
protected $cityRepository;
- Declare class members & functions as
protected
orpublic
unless really required asprivate
. Private variables are accessible only using other functions. - Define enumerations instead of using Config Values. For eg: Status ("Pending", "Success").
abstract class DaysOfWeek
{
const Sunday = 0;
const Monday = 1;
// etc.
}
$today = DaysOfWeek::Sunday;
- Config & Route caching should only be done on production.
Comments are mandatory to provide information about arguments and return types.
Your code should basically speak for itself, however, the need for comments may arise when the logic inside the function is quite complex and actually needs explanation. Use single line (//
) comments for that (to explain line by line)
This
/**
* Register a binding with the container.
*
* @param string|array $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
* @return void
* @throws \Exception
*/
or this
/**
* Register a binding with the container.
*
* @param string|array $abstract
* @param \Closure|string|null $concrete
* @param bool $shared
*
* @return void
*
* @throws \Exception
*/
The difference between the two is spacing and new lines.
- Use
bool
instead ofboolean
. - Provide full path to Class. For eg.
\App\Service\UserService
rather than justUserService
. - Avoid using
mixed
as a@param
or@return
value until necessary. - Comment as you go! Don't think taking out time separately for this will be any helpful. You'll probably forget what the function does or how it works and spend time reading your own code to write that comment.
- Inline comments are mandatory for complex logic. Its easy for you to understand your own code, but can be difficult for others.
- Routing
- Points To Remember
- Method Types
- Rest Approach
- Grouping
- Naming
- Binding Models
- Simple Binding
- Nested Binding
- Custom Routers
- Without Namespace
- With Namespace
- Points To Remember
- Requests
- Handling
- Authorization & Validation (Form Requests)
- Authorization & Validation (MVC)
- Response
- Macros
- Non-Modular Way
- Modular Way
- Macros
- Exceptions
- Basic PHP Exceptions
- Basic Laravel Exceptions
- Pre-Defined
- Docs
- Types
- Custom Exceptions
- Handling Exceptions
- The Switch Case Approach
- Separating Web & API Requests (5.7 and lower)
- Collections
- Macros
- Non-Modular Way
- Modular Way
- Macros
- Helpers
- Defining
- Autoload Using Composer
- Hacks
- Tips
- Tricks
- LaravelPHP (Laravel - Twitter)
- LaravelDaily (Tuts & Tips - Twitter)
- Taylor Otwell (Creator @Laravel)
- Jeffrey Way (Founder @Laracasts)
- Phil Sturgeon (Founder @PyroCMS)
- Adam Wathan (Founder @TailwindCSS)
- Matt Stauffer (Founder @LaravelPodcasts)
- Graham Campbell (Contributor @Laravel)
- Caleb Porzio (Laravel Evangelist)
- Marcel Pociot (Laravel Evangelist)
- Alex Garrett (Founder @CodeCourse)
- Freek van der Herten (Founder @Spatie)