Laravel: GlobalController with Auth - php

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

Related

problems with routes laravel

After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."
My Admin Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
class AdminController extends Controller
public function store(Request $request)
{
// Validation code
// Saveing code
return redirect()->route('pages.admin')
->with('success', 'Admins created successfully.');
}
My Page Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controllerpublic
function admin(){
return view('pages.admin');
}
Routes:
Route::get('/admin', 'PagesController#admin');
Route::post('admin_form', 'AdminController#store');
Would appreciate the help.
I looked in online sources but it didn't help
You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.
For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.
Route::get('/admin', 'PagesController#admin')->name('pages.admin');
It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn't changed.
I found a video and changed my code in the controller to
return redirect('admin');
and it worked.

How to delete a post if its the user in eloquent?

the app currently has the ability to delete all tasks, but it needs to delete a task only if its owned by the user that is LOGGED in.
this is in php slim and im using eloquent.
here is what i currently have, it currently deletes all posts even if its not by the user.
Any suggestions, their isn't much documentation on eloquent in regards with slim. so im unsure.
todoscontroller.php
<?php
namespace App\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use Illuminate\Database\Capsule\Manager as DB;
use App\Models\Task;
use App\Models\User;
use App\Auth\Auth;
class TodosController extends BaseController
{
public function deleteTodo($request, $response, $id)
{
// $sth = $this->db->prepare("DELETE FROM tasks WHERE id=:id");
// $sth->bindParam("id", $args['id']);
$owl = $id['id'];
$todos = Task::find($owl);
// $todos = $sth->fetchObject();
// $url = urlFor($todos);
$todos->delete();
return $response->withJson($todos)->withRedirect('/todos');
}
Good Question.
But have you seen the Slim Framework document?
There is a Middleware, and I am sure your could handle your request by the Middleware before it accesses the controller.

Can't Access Auth::check in my view in laravel 5.3

I'm new to laravel framework and trying to implement login system to my website. My web site has a landing page. Base on user login status I need to change this links like bellow.
<ul class="secondary_menu">
#if(Auth::guard('customer')->check())
<li><a href="{{url('/logout')}}" >Logout</a></li>
#else
<li><a href="{{url('/login')}}" >Login or Register</a></li>
#endif
</ul>
Here's the controller responsible for loading this view
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class CustomerController extends Controller
{
public function index()
{
return view('public.index');
}
}
I'm getting an error that saying that Class 'Auth' not found
I try adding use Auth and use \Auth as suggested in other slimier kind of questions in stackoverflow but did not works for me. What am i doing wrong. Isn't is possible to check call Auth from view ?
Put the following code to use the Auth in your code:
use Illuminate\Support\Facades\Auth;
and then use it like:
if (Auth::check())
{
// your code
}
As alternative to Auth:: facade you can use global helper:
auth()->check();
auth()->user();
Try this on your controller
use Illuminate\Support\Facades\Auth;

Laravel Authentication not recognising on nested controller

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...

laravel5.2 Auth::guest() returns error "Class 'App\Http\Controllers\Auth' not found"

I am trying to use the inbuilt Auth for login in laravel and register. These automatically generated pages work so well, now I use Auth::guest() to check if the user is authorized to return a view: index else to login page.
But it shows:
Class 'App\Http\Controllers\Auth' not found".
Here's the code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
class StepsController extends Controller
{
public function step1()
{
if (Auth::guest())
{
return redirect('login');
}else {
return view('index');
}
}
}
You need to "import" the definition of the facade of Auth and remove the use App\Http\Controllers\Auth because does not exists.
Just add at the top (just before the class declaration):
use Auth;
And remove the other:
Having this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
class StepsController extends Controller
{
....
Hope it helps.
PS: If you want to learn Laravel with a good guide, step-by-step, please check here: Learn Laravel

Categories