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');
});
Related
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');
}
}
I have my controller ExampleController:
class ExampleController extends Controller
{
function __construct()
{
$this->middleware('auth:student')->only(['store', 'update', 'destroy']);
}
public function index()
{
if(CheckUser::student()) {
dd("Is student");
}
dd("Isn't student");
}
/**
* Another method's not relevant.
**/
}
I'm trying to add some logic if is student.
But have one problem, I just can access: Auth::user() if I set the middleware. But this specific method can be accessed without has logged in.
My question
Is possible to create a not required middleware for what if logged get user information?
Note: I'm using Laravel passport with multi auth.
If you want to protect specific methods that are in your controller and leave out others in the same controller
You may try to protect your endpoint on the routes files (api.php or web.php) rather than on the Constructor
//protected routes
Route::group(['middleware' => 'auth:api'], function () {
Route::post('customer/picture/add', 'Mobile\AuthController#addProfilePicture');
Route::post('customer/phone/update', 'Mobile\AuthController#updatePhoneNumber');
});
//unprotected routes
Route::get('customer/login', 'Mobile\AuthController#getLoginForm');
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...
Lately, I've been creating only models for my L5 packages, but now I'd like to try and introduce a Controller as well. I've been following this but I always get the error "Class StatController does not exist".
Folder structure
/src
routes.php
/Controllers
StatController.php
StatController.php
<?php namespace Enchance\Sentrysetup\Controllers;
use App\Http\Controllers\Controller;
class StatController extends Controller {
public function index() {
return 'Moot.';
}
}
Service provider
public function register()
{
// Can't get this to work
include __DIR__.'/routes.php';
$this->app->make('Enchance\Sentrysetup\Controllers\StatController');
$this->app['sentrysetup'] = $this->app->share(function($app) {
return new Sentrysetup;
});
}
routes.php
Route::get('slap', 'StatController#index');
Does anyone have an alternate way of assigning a Controller to a L5 package?
You don't need to call $this->app->make() on your controllers. Controllers are resolved by Laravel's IoC automatically (meaning that Laravel creates / instantiates controllers automatically that are tied to routes).
Require your routes in the boot() method of your packages service provider:
public function boot()
{
require __DIR__.'/routes.php';
}
And inside your routes.php file:
Route::group(['namespace' => 'Enchance\Sentrysetup\Controllers'], function()
{
Route::get('slap', ['uses' => 'StatController#index']);
})
Also, just a tip. You should PascalCase your namespaces:
Enchance\SentrySetup\Controllers
Note the capital S in setup
The boot() method should be used to register your routes because when Laravel starts up, it goes through each service provider in your config/app.php file, creates them, calls the register() method (to insert/add any dependencies that the service provider "provides" into Laravel's singleton / IoC container).
Think of Laravel's container as simply a big key => value array. Where the "key" is the name of the dependency (such as config), and the "value" is a closure (function () {}) that is called to create the dependency:
// Exists as a property inside the Container:
$bindings = [
"config" => function () {
// Create the application configuration.
}
];
// Create the app configuration.
$config = $bindings['config']();
// Create the app configuration (Laravel).
$config = app()->make('config');
Once Laravel has registered each provider, it then goes through them again and calls the boot() method. This ensures that any dependencies that have been registered (inside of the register() method of all of your application service providers) is available in the boot() method, ready to be used.
The Service Provider Boot Method - Laravel Docs
Use this in controller function:
use Illuminate\Routing\Controller;
Like:
namespace Enchance\Sentrysetup\Controllers;
use Illuminate\Routing\Controller;
class StatController extends Controller {
public function index() {
return 'Moot.';
}
}
And in controller:
Route::group(['namespace' => 'Enchance\Sentrysetup\Controllers'], function()
{
Route::get('slap', 'StatController#index');
});
In boot function:
public function boot()
{
require __DIR__.'/routes.php';
}
Its might be a bug in laravel but not sure, need your suggestions about to resolve this.
The issue is when you use more than one controller under route:group with controller subdirectory except one controller other would be 404s.
Here's my detail code review for senerio:
Routes
#routes.php
#Settings
Route::group(array('prefix' => 'setting'), function() {
#Index
Route::controller('/', 'Setting\IndexController',[
'getIndex'=>'index/setting'
]);
#company detail
Route::controller('company', 'Setting\CompanyController',[
'getInfo'=>'info/company',
'getEdit'=>'update/company'
]);
});
Controllers
IndexController.php
#/app/Http/Controllers/Setting/IndexController.php
namespace App\Http\Controllers\Setting;
use App\Http\Controllers\Controller;
class IndexController extends Controller {
public function getIndex(){
return "setting.index";
}
}
CompanyController.php
namespace App\Http\Controllers\Setting;
use App\Http\Controllers\Controller;
class CompanyController extends Controller {
public function getInfo(){
return ('setting.company.index');
}
public function getEdit(){
return ('setting.company.edit');
}
}
Currently its not working but when you comment one route::controller other will work fine and viceversa.
and also if remove one route:controller and add route like:
Route::get('/', array('as' => 'index/setting', 'uses' => 'Setting\IndexController#getIndex'));
than both will work fine.
But I need to use route:controller for all controllers under route:group.
So, if still there something missing to explain let me know, I will update further in depth.
Any help would be appreciable.