vendredi 22 janvier 2016

Extract to a single method Laravel 5.2

I have a ProfileController right now that is checking if the current user is = to the user on the page, if not it redirects them. As you can see I have that call attached into every single method.

 if( Auth::check() && Auth::user()->username != $username) {
            // not authenticated user, so access is denied
            return redirect('/');
        }

Is there a way I can extract it to one method, (like my User.php Model Class) and then just call it with that method, instead of repeating the same process.

If so, how?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;


class ProfileController extends Controller {


    /**
     * Get the current users profile.
     */
    public function getProfile($username) {

        // If the current username is NOT equal to the currently logged
        // in users username, then redirect them back.
        if( Auth::check() && Auth::user()->username != $username) {
            // not authenticated user, so access is denied
            return redirect('/');
        }

        // Check if user exists
        // Set 'username' = $username, with first result
        $user = User::where('username', $username)->first();

        // Return view with user.
        return view('profile.index')->with('user', $user);

    }



    /**
     * Edit your Profile
     */
    public function editProfile($username) {
        // If the current username is NOT equal to the currently logged
        // in users username, then redirect them back.
        if( Auth::check() && Auth::user()->username != $username) {
            // not authenticated user, so access is denied
            return redirect('/');
        }

        // Check if user exists
        // Set 'username' = $username, with first result
        $user = User::where('username', $username)->first();

        // Return view with user.
        return view('profile.edit-profile')->with('user', $user);
    }


    /**
     * Show your Travel Flyers in Your Profile Page
     */
    public function showYourFlyers($username) {
        // If the current username is NOT equal to the currently logged
        // in users username, then redirect them back.
        if( Auth::check() && Auth::user()->username != $username) {
            // not authenticated user, so access is denied
            return redirect('/');
        }

        // Check if user exists
        // Set 'username' = $username, with first result
        $user = User::where('username', $username)->first();

        // Return view with user.
        return view('profile.your-flyers')->with('user', $user);
    }


}

EDIT****** You mean some thing like this:

// Get Profile Dashboard
    Route::get('/user/{username}', [
        'uses' => '\App\Http\Controllers\ProfileController@getProfile',
        'as'   => 'profile.index',
        'middleware' => ['auth'],
    ]);



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1ZHMYmR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire