php - Laravel 5 Auth Redirecting - It works, but is it correct? -
let me try explain this.
if guest goes /
directory, welcome. if guest tries go /start
directory, redirected log in page.
after guest logs in, redirected /start
directory. if logged in user goes /
directory, again redirected /start
directory. (no need see home page once logged in).
i got working how want, i'm not sure if i'm doing how laravel intends done being there non-dry code in pagescontroller.php
. plus, i'm redirecting pagescontroller pagescontroller (doesn't sound follows practices me).
here routes.php
:
<?php route::get('', 'pagescontroller@index'); route::get('start', 'pagescontroller@start'); route::controllers([ 'auth' => 'auth\authcontroller', 'password' => 'auth\passwordcontroller' ]);
here pagescontroller.php
:
<?php namespace app\http\controllers; use app\howitworksmodel; use app\whatyougetmodel; use app\startcontentmodel; use illuminate\http\request; use auth; use app\http\requests; use app\http\controllers\controller; class pagescontroller extends controller { public function index() { if (auth::check()) { return redirect()->action('pagescontroller@start'); } $howitworkscontent = howitworksmodel::all(); $whatyougetcontent = whatyougetmodel::all(); return view('pages.index', compact( 'howitworkscontent', 'whatyougetcontent' )); } public function start() { if (auth::check()) { $startcontent = startcontentmodel::all(); return view('pages.start', compact( 'startcontent' )); } return redirect('/auth/login'); } }
how can restructure work works having better practices in mind? or i'm doing okay these purposes?
it better done middlewares, using middlewares take authentication resposability of controllers , let worry content, make controllers light , maintainable.
you should use auth middleware
Comments
Post a Comment