Laravel 5.6 get user in Routes issue - php

How to get an id of the user?
app/routes/web.php
Route::group(['middleware' => 'auth'], function()
{
var_dump(Auth::user());
}
It returns null.
Simple things made difficult.. Also there is no reliable explanation
How to handle this?

You have not defined any route, try this instead:
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
dd(Auth::user());
});
});
Change the '/' to what ever you like, does that work for you?

Related

Laravel routing without appending any prefix

I have a path in Laravel it is like subdomain.mydomain.com/admin/login
I am trying to call
subdomain.mydomain.com and need to get the login page straight.
Currently, it's not working
This is the function I am using in routerserviceprovider.php
protected function mapAdminRoutes()
{
Route::middleware('subdomain.mydomain.com')
->prefix('admin')
->namespace($this->namespace)
->group(base_path('routes/admin.php'));
}
and in admin.php there is a resource group shows like this:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
//Login Routes...
Route::view('login','admin.login');
});
can anyone help with this?
Add following route
Route::get('/',function(){ return view('login.index'); })->name('admin.login');
i hope it helps

How to Map new route to specific old route in Laravel?

I have following route group for admin panel
Route::prefix('admin')->group(function (){
.
.
.}
I want to wrap this route to a new route e.g asda12asda
so that old behavior :
/admin/users
is changed to :
/asda12asda/users
not allowing old route. I don't want to change it internally from the system and want to find some efficient Laravel way to achieve it.
Redirect the old route to a new route
Route::prefix('admin')->group(function (){
Route::any('login', function () {
// Redirect to new route
redirect()->route('new route');
});
});
by creating a new route and mapping it accordingly
Route::prefix('asda12asda')->group(function () {
Route::any('login', function () {
// Do whatever you were about to do
})->name('new route');
});
If you are using Laravel 5.4 then you can add a new route file. Assume your route name is
adsp.php then add it to RouteServiceProvider.php like this.
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/adsp.php.php');
});
}

Using the same route for different types of users in Laravel

My application will have doctors, patients and administrators
Now I would like to have the same route for all roles. as following:
app.com/dashboard/
app.com/dashboard/appointments
But I would like to have a different Controller for the one used for patients, and the one for doctors offcourse. I have tried the following without success
Route::group(['prefix' => 'dashboard', 'middleware' => ['auth']], function () {
/* Doctor */
Route::group(['middleware' => ['role:doctor']], function() {
Route::get('/appointments', 'Doctor\AppointmentController#index');
});
/* Patient */
Route::group(['middleware' => ['role:patient']], function() {
Route::get('/appointments', 'Patient\AppointmentController#index');
});
});
I get an 403 (Unauthorized) error caused by my Role middleware. Even without that I think it's just overwriting the other?
Has anyone been able to use the same route (/dashboard) for completely different purposed? aka different controllers/views
Thank you!
You can use a workaround for this by checking the user role and then call the controller function accordingly as:
Route::get('appointment', function()
{
if (auth()->user()->isDoctor())
{
app()->call('Doctor\AppointmentController#index')
}
elseif(auth()->user()->isPatient())
{
app()->call('Patient\AppointmentController#index')
}
});

Laravel redirect to route

I'm using Laravel 5.1 and wanted to know if there is a better way to do routing redirection.
Route::get('user/login', 'UserController#login');
Route::get('login', function() {
return redirect()->to('user/login');
});
So basically, whenever a user goes to http://example.com/login, they will be redirect to http://example.com/user/login.
Is there a better way or other ways to do this or am I doing it correctly already? Thanks!
That's about as simple as it gets
You could also do redirect('user/login') to save a few characters
If you had multiple redirects like this you could handle them all at once
Route::pattern('user_path', '(login|logout)');
Route::get('{user_path}', function($user_path) {
return redirect('user/' . $user_path);
});
Route::get('new/create_view','CreateController#create_view');
Route::post('new/create_view','CreateController#insert_view');
Route::get('new/create_table','CreateController#create_table');
Route::post('new/create_table','CreateController#insert_table');
Route::get('new/create_package','CreateController#create_package');
Route::post('new/create_package','CreateController#insert_package');
This is the way i am using the route. Simple method. when the time of GET, am calling a controller function and inside that particular controller function, i have written the logical codes. In the POST also, doing the same thing.
Another way is there,GROUP Routing
Route::group(['namespace' => 'Admin'], function()
{
// Controllers Within The "App\Http\Controllers\Admin" Namespace
Route::group(['namespace' => 'User'], function()
{
// Controllers Within The "App\Http\Controllers\Admin\User" Namespace
});
});
eg:
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
Route::resource('pages', 'PagesController', array('only' => array('index', 'store', 'show', 'update', 'destroy')));
Route::resource('users', 'UsersController');
});

Laravel nested route group inverse middleware

I have a fairly complex application which makes use of custom authentication middleware. I have a route group like this:
Route::group(['prefix' => 'auth', 'middleware' => 'auth'], function() {
Route::get('/', function() {
echo 'private';
});
Route::group(['prefix' => 'public'], function() {
Route::get('/', function() {
echo 'public';
});
})
});
Now the auth middleware will redirect all requests which are not authenticated. The main group with the auth prefix is authenticated. However, I want the group public to be accessible even if the user is not authenticated.
So:
http://example.com/auth < must be authenticated
http://example.com/auth/some-sub-page < must be authenticated
http://example.com/auth/public < no need for authentication
So is there a way to add something like 'remove-middleware' => 'auth' to the nested group with the public prefix? Or would I have to restructure my route groups?
Why not just wrap the auth middleware around the non-public routes?
Route::group(['prefix' => 'auth'], function() {
// routes requiring auth
Route::group(['middleware' => 'auth']) {
Route::get('/', function() {
echo 'private';
});
}
// other routes
Route::group(['prefix' => 'public'], function() {
Route::get('/', function() {
echo 'public';
});
});
});
There may be a way to do a 'remove-middleware' type middleware, but that could get messy. This seems like the cleanest solution.
you can use withoutMiddleware method on your route.
Route::get('/example')->withoutMiddleware('auth')
You can also use withoutMiddleware on Route::group

Categories