i found a issue it say 'Class admin does not exist'. Anything I missing for the issue? Thanks.
Here is my Route.php
Route::group(['prefix' => 'admin', 'middleware'=> ['auth' => 'admin']], function () {
Route::get('/','AdminController#index');
Route::get('profile','AdminController#profile');
Route::get('/addProduct','AdminController#addProduct');
});
Here is my AdminController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function index(){
return view('admin.index');
}
public function profile(){
return view('admin.profile');
}
public function addProduct(){
return view('admin.addProduct');
}
}
Issue with in the route file to assign middlewares to route group
If you have two middlewares then assign like this ["auth", "admin"] instead of ["auth" => "admin"].
Route::group(['prefix' => 'admin', 'middleware'=> ['auth', 'admin']], function () {
Route::get('/','AdminController#index');
Route::get('profile','AdminController#profile');
Route::get('/addProduct','AdminController#addProduct');
});
Related
How i get all users in laravel 5.3 . i am using barryvdh for cors
this is my RegisterController which is in Auth folder
public function index(){
return Users::all();
}
above code gives all the user data if in route below we do not use middleware
and if we use middleware then i got error unauthenticated . so i want to get all user data using middleware in route . How can i get
Route::get('/users','Auth\RegisterController#index')->middleware('auth');
I think you can try this :
public function index(){
$users = Users::get();
return $users;
}
Hope this work for you!
So if you want to get all data of your user table you simple have to do following:
# Middleware group if user is logged in
Route::group(['middleware' => 'auth'], function ()
{
Route::get('home', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::group(['prefix' => 'user'], function ()
{
Route::get('get', ['as' => 'getUser', 'uses' => 'UserController#getUser']);
});
});
And in your controller you can do something like this:
class UserController extends Controller
{
public function getUser(Request $request)
{
$users = DB::table('users')->get();
return $users;
}
}
If you want add something to that return you probably have to create a relation between your models, and call them in your method and return them.
If you still have any questions or if I understood something wrong feel free to comment on this answer.
Edit:
If you want to return all user with an api route you can do following in your api routes:
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Try this
public function index()
{
$userDetail = Users::get();
dd($userDetails);
}
I have a problem that I can not resolve in Laravel 5.4.
I'm using the Postman extension to make requests for my API, so far it works normally with GET, but when I try to do a POST, the method that's actually called is GET again. (The API can not have authentication or token for the user).
api.php:
<?php
use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::group(['api' => ['auth:api']], function(){
Route::group(['prefix' => 'user'], function(){
Route::get('{id}', ['uses' => 'UserController#getUser']);
Route::post('', ['uses' => 'UserController#saveUser']);
Route::get('', ['uses' => 'UserController#allUsers']);
Route::put('{id}',['uses' => 'UserController#updateUser']);
Route::delete('{id}', ['uses' => 'UserController#deleteUser']);
});
});
UserController.php:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller{
protected $user = null;
public function __construct(User $user){
$this->user = $user;
}
public function allUsers(){
return $this->user->allUsers();
}
public function getUser($id){
}
public function saveUser(){
return $this->user->saveUser();
}
public function updateUser($id){
}
public function deleteUser($id){
}
}
User.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public $hidden = ['venda','remember_token', 'created_at','updated_at'];
public $fillable = ['nome','email', 'venda'];
public function allUsers(){
return self::all();
}
public function saveUser(){
$input = Input::all();
echo 'aa';
$user = new User();
$user->fill($input);
$user->save();
return $user;
}
}
First change this:
Route::group(['api' => ['auth:api']], function(){
To:
Route::group(['middleware' => ['auth:api']], function(){
Here is my routes file:
Route::get('/', 'WebController#index');
Route::group(['prefix' => 'cn'], function () {
Route::get('/', 'WebController#index');
Route::group(['namespace' => 'ACL', 'prefix' => 'auth'], function () {
Route::get('login', 'AuthController#login');
Route::post('login', 'AuthController#login');
Route::get('logout', 'AuthController#logout');
});
Route::group(['middleware' => ['auth', 'token']], function () {
Route::get("/intentional", 'IntentionalOrderController#index');
Route::get("/intentional/add", 'IntentionalOrderController#create');
Route::post("/intentional/add", 'IntentionalOrderController#store');
Route::get("/intentional/selfOrder", 'IntentionalOrderController#selfOrder');
Route::post("/intentional/selfOrder", 'IntentionalOrderController#storeSelfOrder');
});
});
Route::group(['prefix' => 'en'], function () {
Route::get('/', 'WebController#index');
Route::group(['namespace' => 'ACL', 'prefix' => 'auth'], function () {
Route::get('login', 'AuthController#login');
Route::post('login', 'AuthController#login');
Route::get('logout', 'AuthController#logout');
});
Route::group(['middleware' => ['auth', 'token']], function () {
Route::get("/intentional", 'IntentionalOrderController#index');
Route::get("/intentional/add", 'IntentionalOrderController#create');
Route::post("/intentional/add", 'IntentionalOrderController#store');
Route::get("/intentional/selfOrder", 'IntentionalOrderController#selfOrder');
Route::post("/intentional/selfOrder", 'IntentionalOrderController#storeSelfOrder');
});
});
The problem is:
When I enter the /en or /cn url, the browser will say too many redirects; but when I enter /en/auth/login or any other url, it will work just fine.
Althought I can fix this by renaming /cn to /cn/index, but still I don't understand why this is not working.
PS:
1. My laravel version is 5.1
2. Debug environment is wamp
3. Browser is chrome
Update:
here is my WebController and BaseController
<?php namespace App\Http\Controllers;
class WebController extends BaseController
{
public function index()
{
return view(config('app.locale') . ".web.index", ["title" => trans('common.smart_fabric'), "active" => "0"]);
}
}
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Request;
class BaseController extends Controller
{
public function __construct()
{
$locale = Request::segment(1);
if ($locale) {
switch ($locale) {
case 'cn':
App::setLocale('cn');
break;
case 'en':
App::setLocale('en');
break;
default:
App::setLocale('cn');
}
}
}
I use Laravel 5.2 and have a problem with middleware.
There is the code in the routes.php
use Illuminate\Contracts\Auth\Access\Gate;
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'HomeController#index');
});
Route::group(['prefix'=>'admin', 'middleware' => 'admin'], function(){
Route::get('/', function(){
return view('admin.index');
});
Route::get('/user', function(){
return view('admin.user');
});
});
Kernel.php:
protected $routeMiddleware = [
...
'admin' => \App\Http\Middleware\AdminPanel::class,
];
AdminPanel.php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Role;
class AdminPanel
{
public function handle($request, Closure $next)
{
$user = Auth::user();
dd($user);
if($user){
$role = Role::whereName('admin')->first();
if($user->hasRole($role)){
return $next($request);
}
}
return redirect('/');
}
So, $user = Auth::user() always return null.
Thanks for suggestions!
I faced a situation where Auth::user() always returns null, it was because I was trying to get the User in a controller's constructor.
I realized that you can't access the authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you can define a Closure based middleware directly in your controller's constructor.
namespace App\Http\Controllers;
use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class ProjectController extends Controller
{
protected $user;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->user = Auth::user();
return $next($request);
});
}
}
Any route that uses Auth() must be encapsulated in the web middleware. You're close, just move your Route::group(['prefix' => 'admin'], ...) into the group above.
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', 'HomeController#index');
// Moving here will ensure that sessions, csrf, etc. is included in all these routes
Route::group(['prefix'=>'admin', 'middleware' => 'admin'], function(){
Route::get('/', function(){
return view('admin.index');
});
Route::get('/user', function(){
return view('admin.user');
});
});
});
Define middleware in the constructer of your controller and it will do the trick here
public function __construct()
{
$this->middleware('auth:api');
}
I had the same problem because i did not set the table name.
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'users';
I found a solution in an old code
function getAuth($guard, $get)
{
return auth($guard)->user()->$get;
}
add this ^ as a helper function and use it wherever you want
ex:
getAuth('user', 'id');
just include your authentication middleware in call
$user = auth('middleware')->user()
Route::middleware('auth:api')->group(function () {
Route::get('/details', 'UserController#details');
});
My Auth::user() return null in view when
I don't have users table in database
I don't have id field as primary key of table users
Don't working authentication. I create authentication manually.
My AdminController:
class AdminController extends Controller
{
public function signin() {
return view('admin.signin');
}
public function index(Request $request) {
dd(Auth::check());
if (Auth::check())
return view('admin.index.index', ['login' => Auth::user()->name]);
else
return redirect()->action('AdminController#signin');
}
public function login() {
$data = Input::all();
if (Auth::attempt(['name' => $data['login'], 'password' => $data['password']])) {
return redirect()->intended('/admin');
} else {
return redirect()->intended('/admin/signin');
}
}
public function logout() {
if (Auth::logout() ) {
return Redirect::to('/admin');
}
}
}
My routes.php file:
//GET
Route::get('/', 'IndexController#index');
Route::get('/admin/signin', 'AdminController#signin');
Route::get('/admin', 'AdminController#index');
Route::get('/admin/logout', 'AdminController#logout');
//POST
Route::post('/admin/auth', 'AdminController#login');
dd(Auth::check()); returned false
What I doing wrong?
In Laravel 5.2 you need to define routes using web middleware to make sessions work, so your routes.php file should look like this:
Route::group(['middleware' => ['web']], function () {
//GET
Route::get('/', 'IndexController#index');
Route::get('/admin/signin', 'AdminController#signin');
Route::get('/admin', 'AdminController#index');
Route::get('/admin/logout', 'AdminController#logout');
//POST
Route::post('/admin/auth', 'AdminController#login');
});