Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I want auth()->user() for do some operation in handler.php file that inside App\Exceptions.
I have done research on it but not find anything.
This is probably because user is still not authenticated before it gets to the handler.
This is desired behavior, but there are some workarounds as stated in this answer here: https://stackoverflow.com/a/41400225/7770919
What #geertjanknapen is referring to is this: https://laravel.com/docs/9.x/errors#rendering-exceptions
You can use auth()->user() in render methods for any exceptions.
Such as:
use App\Exceptions\YourCustomException;
/**
* Register the exception handling callbacks for the application.
*
* #return void
*/
public function register()
{
$this->renderable(function (YourCustomException $e, $request) {
$authenticatedUser = auth()->user();
return response()->view('errors.invalid-order', [], 500)->with('user', $authenticatedUser);
});
}
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
I have a single form.blade.php that I use to create and modify data. In that form, sometimes I have to display additional fields only in the editing state, while in the creation state they should be hidden.
I tried to define variable $mode with values edit and create in each method and it works ok. From a developer's point of view this seems like a bad solution and I wanted to consult with you on how I could do it in a better way
What i do in controller
public function edit($id)
{
$tag = Tag::find($id);
$mode = 'edit';
return view('tag.edit', compact('tag', 'mode'));
}
public function create()
{
$tag = new Tag();
$mode = 'create';
return view('tag.create', compact('tag', 'mode'));
}
And than in form.blade
#if($mode == 'edit')
....
#else
....
#endif
Do you think there is a better way to do this?
Since the create and edit mode are separate, you can send the mode right when you include the form blade
tag.edit.blade.php
#include('tag.form', ['mode' => 'edit'])
tag.create.blade.php
#include('tag.form', ['mode' => 'create'])
So you dont have to specify it in the controller.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an array in my command and I want to return the array to a controller where I call the command. This is what I want.
Command :
return $array;
Controller :
$array = Artisan::call('command');
I have also tried this:
Artisan::call('command');
$array = Artisan::output();
But they both don't work. Please help?
You can't get the result of your command back in your controller, (you will only get the "text" from it)
Instead if you want directly a php array, try to export your command's code in a helper function (or in the model you are using in your command), then call it from your controller and your command.
If you can't do that, try this in your handle() function:
$this->line($array->toJson());
then in your controller :
Artisan::call('command');
$json = Artisan::output();
$array = json_decode($json);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
In a Laravel 5.1 project I have:
Videos that belong to Pages and Pages that belongs to Users.
I want to make sure Users can't Videos and Pages not owned by them. Admin users can see all Pages and Videos.
How to solve this?
I am already using a middleware and passing User ID as a parameter, but I don't know how to get the User ID inside the Video Constructor.
I probably need something like Constructor Injection, but the documentation is not really clear.
EDIT:
As recommended by #tommy I used Laravel authorization to check if user had permission or not.
I decided to use a single repository, since the check would always to be the same (matching pages with user)
In \App\Providers\AuthServiceProvider
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
$gate->define('check-owner', function ($user, $page) {
return $user->id === $page->user_id;
});
}
In App\Repositories\OwnerRepository
namespace App\Repositories
use App\Page;
use Gate;
class OwnerRepository
{
public function CheckifOwns(Page $page){
if (Gate::denies('check-owner', $page))
{
abort(403, 'Unauthorized action.');
}
}
}
In Videos Controller
use App\Repositories\OwnerRepository;
private $repository;
public function __construct(OwnerRepository $repository)
{
$this->repository = $repository;
$this->middleware('auth');
}
public function show($id)
{
$video= Video::findOrFail($id);
$this->repository->CheckifOwns($video->Page);
return view('videos.view',compact('video'));
}
A new Authorization system has been introduced with Laravel 5.1.11. You can use Policies to easily check if a user is allowed to access a specific resource:
http://laravel.com/docs/5.1/authorization
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have tried the following code but is show some error. Please tel me whats wrong with the following code?
<?php
public $PreviousId;
public $HasNextcandidate= $this->HasCandidateValue();
public function HasCandidateValue()
{
return ($PreviousId!=NULL);
}
?>
You can't assign values to member variables using expressions including function/method calls.
So public $HasNextcandidate= $this->HasCandidateValue(); is not valid. You need to do something like:
public $HasNextcandidate= '';
and then in your constructor do:
$this->HasNextcandidate = $this->HasCandidateValue();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I got the idea down, and to be so simple, I think it may be impractical, and so I would comment on the pros (if any) and cons of such a system.
The intention was to develop a simple but functional system permissions.
The problem I could see is that if you want to protect a particular function, you have to suddenly rename it to have the conf_ prefix.
I would write a proxy around a class that inspects the doc blocks when a method is invoked. Just an idea:
class ArticlesProxy
{
private $backend;
public function __construct(articles $backend)
{
$this->backend = $backend;
}
public function __call($fn, $args)
{
$rm = new ReflectionMethod($this->backend, $fn);
if (strpos($rm->getDocComment(), 'protected') !== false) {
// this method is protected by whatever
}
// perform the proxy call
return $rm->invokeArgs($args);
}
}
$proxy = new ArticlesProxy(new articles());
$proxy->create(1, 2, 3);