I want to use middleware in Usercontroller on only getDashboard() and getUserlist() method, but it didn't work for me.
also I add in controller constructor.
My Controller:
class Usercontroller extends Controller {
public function __construct() {
$this->middleware('auth',['only' => ['getDashboard']]);
//$this->middleware('auth');
}
##This method render home view.
public function index() {
// dd('yahoo');
return view('welcome');
}
##To add new user.
public function getAdduser() {
return view('register');
}
#To render dashboard view.
public function getDashboard() {
return view('dashboard');
}
When I directly type in url http://localhost/rabble/index.php/user/dashboard.it still display display without authenticate user is login or not.
Related
I created a resourceful Laravel livewire component instead of going the controller route and was wondering if this practice is a clean approach? I've also eliminated the render method and have 3 methods that display a view. the problem with this I've noticed is that the wire:submit.prevent won't work.
for example routes
Route::resource('products', 'ProductComponent');
Component:
class ProductComponent extends Component
{
public function index()
{
return view('mypage');
}
public function create()
{
return view('create page');
}
public function store()
{
//
}
public function edit(Model $model)
{
return view('edit page');
}
public function update()
{
//
}
public function destroy(Model $model)
{
//
}
}
In web.php I have this route which opens a form:
$this->namespace('Users')->prefix('users')->group(function (){
$this->get('/create' , 'UserController#create');
});
And this route returns an array of countries. I use that array to fill a select box via ajax in the form.
Route::namespace('API')->prefix('api')->group(function () {
$this->get('/get-country-list', 'LocationsController#index');
});
Controller:
app\Http\Controllers\API\LocationsController
class LocationsController extends Controller
{
public function index()
{
return DB::table('countries')->pluck("name","id")->all();
}
...
app\Http\Controllers\Users\UserController
class UserController extends Controller
{
public function create()
{
return view('panel.users.home.create.show');
}
...
How can I call LocationsController#index in create() function?
what is the best method?
you can try return redirect(route('...')); instead of return view() in actions.
update
Because you just want to get Countries list instead of redirection. So do small tuning, separate the data manipulating function from the action function:
protected function getCountries() {
return DB::table('countries')->pluck("name","id")->all();
}
function index(Request $request) {
return $this->getCountries();
}
function create(Request $request) {
$countries = $this->getCountries();
return view('panel.users.home.create.show', compact('countries'));
}
I think you should try a different approach. What you seem to be trying to do is reuse this cumbersome query:
DB::table('countries')->pluck('name', 'id')->all();
That's good! However your index() function is a controller endpoint and which returns a response and isn't really suitable for being reused in other controller endpoints. When I am in a similar situation I usually do one of two things,
1. Extract the code to a protected method and use it in both controller endpoint methods
class UserController extends Controller
{
public function index()
{
return $this->countryNames();
}
public function create()
{
// $countryNames = $this->countryNames():
return view('panel.users.home.create.show');
}
public function countryNames()
{
return DB::table('countries')->pluck('name', 'id')->all();
}
}
2. Create a method on the model, in your case this would involve using the model instead of the DB facade.
class UserController extends Controller
{
public function index()
{
return Country::names();
}
public function create()
{
// $countryNames = Country::names();
return view('panel.users.home.create.show');
}
}
What I want to do is to get the a User's activation status before running any methods and redirect if they're not active. Here's my code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class HomeController extends BaseController
{
public function __construct(){
parent::__CONSTRUCT();
$this->middleware('auth');
//SEE IF ACTIVE, something like auth()->user()->active
}
public function home()
{
return redirect('/home');
}
}
Look at the comment on the last line of the constructor, how do I do that?
From 5.3 onwards, you can't directly access session info in a controllers constructor. You can, though, define a Closure based middleware directly in your controller's constructor. More info in the docs
public function __construct()
{
$this->middleware('auth');
$this->middleware(function ($request, $next) {
if(Auth::user()->active) {
return Redirect::route('activate');
}
return $next($request);
});
}
Been searching hours for solution online but could not find a solution to this problem:
BadMethodCallException in RedirectResponse.php line 228:
Method [guest] does not exist on Redirect.
This is my controller:
class MemberController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('member.home');
}
}
class SessionController extends Controller
{
public function __construct()
{
$this->middleware('guest', ['except' => 'destroy']);
}
public function create()
{
return view('session.create');
}
}
This is my routes/web.php:
Route::get('/member', 'MemberController#index');
Route::get('/login', 'SessionController#create')->name('login');
When I try to access 127.0.0.1/member, the above error pops up.
Any idea?
you are setting your /member route to point to create method, which is not exists in your member controller object,
this line:
Route::get('/member', 'MemberController#create');
you may need to change it to :
Route::get('/member', 'MemberController#index');
OR
by changing your index method name in your member controller, or creating new method called create if you are using index method in another context:
public function index()
to be :
public function create()
Let's say I have a UsersController. In that controller there is a handler for website.com/users/login and website.com/users/register
How would I handle a route of website.com/users within the controller similarly how I would with the other handlers?
In routes.php:
Route::controller('users', 'UserController');
In UserController.php:
class UserController extends BaseController {
public function getIndex()
{
# GET website.com/users
}
public function getLogin()
{
# GET website.com/users/login
}
public function getRegister()
{
# GET website.com/users/register
}
}
The Laravel docs have more examples: http://four.laravel.com/docs/routing