Laravel Authentication not recognising on nested controller - php

I have a test controller as below:
namespace App\Http\Controllers\Modules\Test;
use App\Http\Requests;
use Illuminate\Http\Request;
use Auth;
use App\Http\Controllers\Controller;
class TestController extends Controller{
public function __construct(){
$this->middleware('auth');
}
public function index(){
return 'testing from controller';
}
}
When I try to access the index method via router, it bounces back as un-authenticated user! although I am already logged in. But it does not recognise as the user is already logged in. It's behave like user has not been authenticated!
Any suggestions what wrong I am doing?
I have tried with middleware group as below:
Route::group(
['middleware' => 'auth'],
function () {
Route::get('testing', 'App\Http\Controllers\Modules\Test\TestController#index');
}
);
Result is same!!!
Any suggestions what wrong I am doing?
Any help will be much appreciated...

Related

Laravel: How to properly rewrite an authentification route in an MVC?

I have to setup my authentication for my reservations system in Laravel.
The basic routing methods by Laravel and a lot of tutorials is by writing the function for the route inside the web.php like this Laraval example:
Route::get('/flights', function () {
// Only authenticated users may access this route...
})->middleware('auth');
However, I was told this isn't the best way to set up routing and that I should use a controller to manage routes.
But that means I have to setup authentification methods inside the controller and to be honest, I don't have the vision to do this..
I thought I could do it like this (My Code)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Auth;
class PagesController extends Controller
{
public function home() {
return view('home');
}
public function reservations() {
return view('reservations')
->middleware(['auth']);
}
public function newreservations() {
return view('reservations_new');
}
}
combined with this web.php setup:
Route::get('/reservations.html', [PagesController::class, 'reservations']);
Route::get('/reservations.html/login', [AuthenticatedSessionController::class, 'create']);
Route::post('/reservations.html/login', [AuthenticatedSessionController::class, 'store']);
Route::get('/reservations_new.html', [PagesController::class, 'newReservations']);
but then I got the error:
Method Illuminate\View\View::middleware does not exist.
So any tips/tricks/methods to do it the right way?
There is no right or wrong way as long as you follow the same principal.
I personally do this:
Route::middleware('auth')->group(function () {
// Authenticated routes
});
you can check this extended discussion
also if you want to use middlewares in controller, this is the right way
class UserController extends Controller
{
/**
* Instantiate a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('log')->only('index');
$this->middleware('subscribed')->except('store');
}
}

Laravel 5.7 Login Form Route

I made a fresh install of Laravel 5.7, and I'm trying to change the view it renders when I go to /login.
When I list my routes, it says that the route /login uses logic from 'LoginController#showLoginForm', but I can't see it in the controller:
php artisan route:list
and when I go to the LoginController, this showLoginForm method doesn't seem to exist...
LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/dashboard';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
Where is the boilerplate that I can change for this?
When you "use AuthenticatesUsers" it extends functionality and also brings the showLoginForm to the controller.
If you want to add code to that function you just need to overwrite it.
If youre using an IDE like PHPStorm you can control click through the "use" declaration to see whats being imported, or you can go manually look inside Illuminate\Foundation\Auth\AuthenticatesUsers
it is in :
use AuthenticatesUsers;
find it and override in loginController
this is that code on vendor:
public function showLoginForm()
{
return view('auth.login');
}
Login form view is located in resources/views/auth/login.blade.php
You can easily change it
showLoginForm() exists on trait AuthenticatesUsers
Just try override showLoginForm() method :)
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/dashboard';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm() {
// Your code
}
}

Laravel: GlobalController with Auth

i will call a Controller all times. It is my GlobalController. I will use the Auth, and DB Function.
I doing this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use DB;
class GlobalController extends Controller
{
function user_in_party() {
// Get the logged user
$user = Auth::user();
print_R($user);
exit;
}
}
Now i call this from my Web.php (Routes) like this but i don't become the Authed user back why?
app('App\Http\Controllers\GlobalController')->user_in_party();
Can u help me? Or say me a better Solution?
Why are you using this Instead you can just Call
Auth::user();
its all global once a user is logged in you can get its details via Auth

Laravel Packages Route

i made some laravel packages, the packages work well if user is not log in, but when user has login and visiting route from packages laravel consider that the user has not log in yet, i ensure that user has successfully log in, because another menu/route not come from the packages it work well. what wrong here ? is i am missing something ?
here is my controller
namespace Offices\Referensi;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ReferensiController extends Controller
{
function tes()
{
echo "Halo";
}
}
here is my service provider
class ReferensiServiceProvider extends ServiceProvider
{
public function boot()
{
include __DIR__.'/routes.php';
}
public function register()
{
$this->app->make('Offices\Referensi\ReferensiController');
}
}
and here is the routes.php
<?php
Route::group(['middleware' => ['auth']], function() {
Route::get('referensi/area','Offices\Referensi\ReferensiController#tes');
});
never mind, i know the problem, i just missing the web middleware on the route file, so the routes.php should be
<?php
Route::group(['middleware' => ['web','auth']], function() {
Route::get('referensi/area','Offices\Referensi\ReferensiController#tes');
});

Laravel 5 registering a controller to map all methods

I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}

Categories