Laravel Episode 11: Updating My Profile


episode 10

Episode 11: Updating My Profile

 

  • Changes in between episode 10 and 11
    • Cleaned up code in routes file.
    • Go through code and replaced Role::userHasRole() with User::hasRoles().
    • Removed __construct() from ProductController.
    • Update database diagram to match current database schema.
  • Create route for /my-profile.
    • Create new route group for auth middleware.
  • Create methods in UserController.
    • myProfile()
    • myProfileUpdate()
      • Make sure to show that the password field is optional.
  • Create view users/my_profile.blade.php.
  • Create link in navbar for my profile interface.

Laravel Episode 10: More Middleware and Routes


episode 10

Episode 10: More Middleware and Routes

 

  • Laravel Debugbar by barryvdh
    • Works with Laravel 4 and 5.
    • Great tool for sql profiler among other things.
  • Review the previous episode regarding multiple roles middleware.
    • Middleware implementation is not the same as how I implemented filters from previous version of Laravel.
    • Use Debugbar how the SQL looks with current implementation of RoleMiddleware().
  • Possibly rewrite Role::userHasRole() to User::hasRoles() like the example from Laravel’s website.
  • Explain how to apply multiple middlewares for each route but for my example, it isn’t working as expected.
  • Restful routing may not be used with implementation that is chosen.
    • Possibly have to recode forms from CRUD to match new routes.
  • Show Route Groups https://laravel.com/docs/5.1/routing#route-groups.

Laravel Episode 9: Middleware Roles

episode 9

Episode 9: Middleware Roles
  • Create a migration file for staff role and default staff account.
php artisan make:migration create_staff_role_and_default_new_user
  • Code the new migration file.
public function up()
{
    // create new staff role
    DB::table('roles')->insert(
        [
            'id'                 => 2,
            'role_name'          => 'staff'
        ]
    );

    // create default user for staff
    DB::table('users')->insert(
        [
            'username'      => 'staff',
            'email'         => 'staff@test.com',
            'password'      => Hash::make('password'),
            'role_id'       => 2,
            'created_at'    => CarbonCarbon::now()->toDateTimeString(),
            'updated_at'    => CarbonCarbon::now()->toDateTimeString()
        ]
    );
}

public function down()
{
    // delete staff user account
    DB::table('users')->where('username', 'staff')->delete();

    // delete staff role
    DB::table('roles')->where('id', '2')->delete();
}
  • Create new middleware for roles
php artisan make:middleware RoleMiddleware
  • Edit Role.php model.
use User;
use Auth;
use Redirect;

public static function userHasRole($role_name)
{
    if (Auth::check())
    {
        $check_role = User::select('roles.role_name')
            ->join('roles', 'roles.id', '=', 'users.role_id')
            ->where('users.id', Auth::User()->id)
            ->where('roles.role_name', $role_name)
            ->first();
        if ($check_role)
        {
            return true;
        }else{
            return false;
        }
    }

    return false;
}
  • Edit the middleware file /app/Http/Middleware/RoleMiddleware.php.
use AppModelsRole;
use Redirect;

public function handle($request, Closure $next, $role)
{
    // check if user has role being checked
    if (! Role::userHasRole($role))
    {
        // redirect to access denied page
        return back()->with('error', 'Access Denied');
    }

    return $next($request);
}
  • Add the new middleware to the /app/Http/Kernel.php.
'role' => AppHttpMiddlewareRoleMiddleware::class,
  • Apply it in a controller.
public function __construct()
{
    $this->middleware('role:admin');
}
  • You can redirect to a custom view if you wish.
  • You can have specific methods apply middleware.

Laravel Episode 8: CRUD Part 2


episode 8

Episode 8: CRUD Part 2

 

  • Go over form method spoofing.
  • Change the create form to use form method spoofing.
  • Delete unnecessary buttons “more info” and “edit”.
  • Create the edit form page.
  • Code the update process.
  • Add the delete functionality and note that it is a POST method.
  • Show how to create a custom_js for blade to put custom javascript code.
  • Add javascript confirmation code for deleting the record.

Laravel Episode 7: CRUD Part 1


episode 7

Episode 7: CRUD Part 1

 

  • Changes since the last episode.
    • Switched out laravelcollective code to standard html code.
    • Moved validation messages to part of the form.
    • Updated elixir to version 5.
  • Create a new migration for products table.
    • id int(11)
    • product_name varchar(255)
    • sku varchar(30)
    • price decimal(5,2)
    • description text
    • timestamps
php artisan make:migration create_products_table
  • Create restful routes for products.
  • Create model for Product.
  • Order of creation.
    • view all page
      • pagination
      • view (show) link for each record
      • if no record exist, show message, else show records
    • create record page
      • form validation

Laravel Episode 6: Password Reset and Email


episode 6

Episode 6: Password Reset and Email

 

  • Move Login/Logout link to the navbar
  • Add password help link to login page
  • Configure .env to work with mailtrap.io
  • Add value to from so that there is a sent from value. Good idea to use env('FROM_EMAIL')
  • Modify route file with the following
// Password reset link request routes
Route::get('password/email', 'AuthPasswordController@getEmail');
Route::post('password/email', 'AuthPasswordController@postEmail');

// Password reset routes...
Route::get('password/reset/{token}', 'AuthPasswordController@getReset');
Route::post('password/reset', 'AuthPasswordController@postReset');
  • Review /Http/Controllers/Auth/PasswordController.php and add the following
use IlluminateHttpRequest;
use IlluminateMailMessage;
use IlluminateSupportFacadesPassword;

use DB;

protected $redirectTo = '/';

public function getEmail() {
	// code here
}

public function postEmail(Request $request) {
	// code here
}

public function getReset($token) {
	// code here
}
  • postReset() is already configured and does not need modification for this demo
  • Create view files
    • auth/password.blade.php
    • auth/reset.blade.php
    • emails/password.blade.php
  • Review /config/auth.php file. Look at the emails.password setting

Laravel Episode 5: MySQL and Auth Design


episode 5

Episode 5: MySQL and Auth Design

 

  • Show how to configure MySQL
  • Create a database
  • Create a user specific to the database
  • Design the Users and Roles table
  • Move roles migration file so it is created before users – rename it
  • Edit user migration file so it uses role_id as foreign key
  • Create log in form
  • Create log in/out controller method
  • Create authenticate controller method
  • Create if/else to show login link or logout link

Laravel Episode 4: Models, Migration, and Database


episode 4

Episode 4: Models, Migration, and Database
  • Look at the /config/database.php file
  • Use sqlite for demo database
touch storage/database.sqlite
  • Look at migration files
  • Explain naming convention
php artisan migrate
  • Explain migration, rollback, and refresh
  • Explain up() and down()
  • Explain what timestamps() is
  • Create a default record for user
  • Create a migration for roles table