php - Laravel 5.2, Load a view based on user information -
this fresh installation of laravel 5.2, thing did add new column table users new column call roles integers...
i'm trying load controller/view base on information of column...
if column roles has value of 1 load view x
in routes file have this
route::group(['middleware' => ['web']], function () { route::auth(); if(auth::user()->roles == '1') { route::get('/admin', 'admincontroller@index'); route::post('/admin', 'admincontroller@save'); route::get('/admin/{datas}', 'admincontroller@datas'); route::get('/admin/list', 'admincontroller@list'); route::get('/admin/list/{details}', 'admincontroller@details'); } else { route::get('/login', 'userscontroller@login'); }
if use auth::user()->roles == '1'
inside view "works" not want , don't want install 3rd packages user control, want simple... how can accomplish this?...
based on matt said(i didn't @ first, i'm new laravel bear me), did...
install fresh laravel...
run php artisan make:auth
, install need users register/login/reset...
open authcontroller.php change protected $redirectto = '/home'
whatever want, in case called delegate $redirectto = '/delegate'
on routers.php file put
route::group(['middleware' => ['web']], function () { route::auth(); route::get('/delegate', 'delegatecontroller@index'); });
you don't need view route, need controller...
on controller delegatecontroller.php put this
namespace app\http\controllers; use app\http\requests; use auth; use illuminate\http\request; // use app\http\controllers\controller; class admincontroller extends controller { public function __construct() { $this->middleware('auth'); } // public function index() { // todo gets data... $datos = ['nombre', 'otro nombre', 'otro mas']; // return data tyhe view... if(auth::user()->roles == '11') { return view('admin.home', compact('datos')); } else { return view('/welcome'); } } }
and thats how different views based on users information... @ least working me..
------------update
after play little bit this, find out that when load correct view admin / user / customer / providers...etc url/path not correct here why:
code above get
foo.com/login when user loads delegation controller serves correct view url stays foo.com/delegate if user admin want foo.com/admin not /delegate , if change manually /delegate /admin still work, here problem because if user not admin still shows admin panel , don't want that... fix had change delegation controller... instead of loading view have redirect user correct path...
public function index() { if(auth::user()->roles == '10') { return redirect()->route('admin'); } else { return redirect()->route('/'); } }
this isthe same code before instead of loading view redirect user, in order work in routes file have declare each name of each path follow
route::group(['middleware' => ['web']], function () { route::auth(); route::get('delegate', 'delegatecontroller@index'); // add route // route::get('admin', 'admincontroller@home'); route::get('admin', ['as'=>'admin', 'uses'=>'admincontroller@home']); });
that way can redirect user part of site calling names route return redirect()->route('/myprofile')
...etc
working need 1 last check user loading what, simple on each of view controllers should have this...
class admincontroller extends controller { public function __construct() { $this->middleware('auth'); // gets disco bouncer working... if(auth::user()->roles != '10') { auth::logout(); } } // public function home() { // todo gets data... $datos = ['nombre', 'otro nombre', 'otro mas']; // todo build model actual data... // return data view... return view('admin.home', compact('datos')); } }
as can see can use on controllers , skip delegationcontroller going have menu each user role on single app.blade master view lot more dirty...
if ask me, yes i'm having fun learning laravel!
Comments
Post a Comment