Skip to content

Instantly share code, notes, and snippets.

@thomasbabuj
Last active August 29, 2015 14:05
Show Gist options
  • Save thomasbabuj/328739b09e7ccc0f02da to your computer and use it in GitHub Desktop.
Save thomasbabuj/328739b09e7ccc0f02da to your computer and use it in GitHub Desktop.
Creating Laravel4 Application
setp 1 : create laravel project
composer create-project laravel/laravel --prefer-dist
note: composer gives two options, --prefer-source and --prefer-dist when install packages
the main difference between these two options ins that with the dist option, composer will favor stable releases and avoid downloading the entire git histroy if possible.
setup 2: composer install
before install , to check if there is are any errors in your composer.json file,
user composer validatie
then, composer install
to update composer.lock file, composer update ( update command will always check for the latest version of every package )
composer diagnose and verbosity flags ( -v | vv | vvv ) can help you identify common problems
1) Define the entities of our application - this will help us when defining the database schema
2) Think about the URL structure of our applciation
Writing first routes:
Route::get ("/", function(){ // return .... });
The first parameter of the get method is the URI pattern. when the pattern is matched the closure function in the second parameter is executed
where() -> needs two parameters, name and a regular expresion pattern
Catching the missing routes
app/start/global.php adding App::missing(function($exception){ return Response::make('page cant found", 404 });
Returing views:
the most frequent object that you will return from your routes is the View object.
Views receives data from a route ( or controller ) and inject it into a template
Creating the Eloquent models
By convention, model names are written in the singular form. a model named "Cat" will map to the Cats table in the database
$fillable array define the list of fields that Larvel can fill by mass assignment, a convenient way to assign attributes to a model.
In the views and controlers of our application, the properties of an Eloquent model can be retrived with the "->" operator
Eg. $cat->name
For the properties of the related models, $cats->breed->name
Creating database schema using artisan
$ php artisan migrate:make add_cats_and_breeds_table
this will create a new migrations inside app/database/migrations
migrations always have an up() and down() method
by conventions, the table and field names are written in "snake_case". Moreover the table names are written in the plural form.
this links has the list of data type laravel supports
http://laravelbook.com/laravel-migrations-managing-databases/
Seeding the database:
Seeder helps add data to our tables
you can also insert bulk insert an array or load data from CSV or JSON file
to controll the order of execution of the seeders, laravel lets you call them individually inside app/database/seeds/DatabaseSeeder.php
we can seed the database by calling,
php artisan db:seed
Blade Templates
Blade is a laravel template engine
only use double braces to output a variable if you trust the user or the value that is returned {{ $var }}
all other cases, use the triple brace notation ( {{{ $var }}}
blade supports all of PHP's major constructs to create loops and conditions :
@for, @foreach, @while, @if and @elseif
These syntax helps in not using opening and closing <?php tags everywhere in your tempalates
Blades let you build hierarchical layouts by allowing the tempaltes to be nested and extended.
@yield directives act ass placeholder for the different sections that a child view can populate and override
@section ... @stop directives delimit the blocks of content that are going to be injected into the master template
@unless (worldIsEnding())
<p>Keep smiling.</p>
@endunless
Unless is the exact opposite of an if statement. An if statement checks if a condition equates to a true value, and then executes some logic. However, the unless statement will execute its logic only if the condition equates to false. You can think of it as a control structure for pessimists.
http://daylerees.com/codebright/blade
Full cheat sheet
http://cheats.jesse-obrien.ca/
When reclone the project to your different machine,
If you get 404 error, then need to update AllowOverRide All in your virtual host
http://www.epigroove.com/blog/laravel-routes-not-working-make-sure-htaccess-is-working
Authenticating users:
Using Auth Class and route filters we can implement users authenticated area
For this we are going to use the Laravel's Users models.
First thing to note is that this model implements the UserInterface. Remember that an interface does not give any
implementations details.
Laravel expects all passwords to be hashed with Hash::make() helper, which uses Bcrypt to create a strong hash
If you were curious as to where and why Laravel uses the make methods in various places, it is only there to maintain PHP 5.3 compatibility, which does not support class member access on instantiation, and therefore, does not let you write return new View('login');
Reference :
http://laravel-recipes.com/recipes/52
http://meanderingsoul.com/dev/2014/05/setting-up-laravel-homestead-on-os-x
https://coderwall.com/p/jpblsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment