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);
Related
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);
});
}
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 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 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 8 years ago.
Improve this question
I have developed a new technique for my future programs to decide which function is to be fired without if/else statements. It is to create an associative array of functions and call them by a function according to argument given. I think actual representation will be better.
$_functions = array(
"names" => function() {
....
},
"orders" => function() {
....
}
);
function doLoad($toLoad) {
global $_functions;
$_functions[$toLoad]();
}
Now, a user has to write only:
doLoad("names");
wherever they want to print out names.
Question:
I will be creating a script and it will be distributed among other fellas to use it their way.
Is it better to do it like this way for my script?
Is there any drawbacks of this technique?
I don't really understand what you are looking for, but I would have done it this way, for more readability. It may also be more efficient :
function doLoadNames() {
}
function doLoadOther() {
}
function doLoad($toLoad) {
$functionName = 'doLoad'.ucfirst($toLoad);
if (function_exists($functionName)) {
$functionName();
return true;
}
return false;
}
all you need is a function called call_user_func or call_user_func_array, manuals are here call_user_func, call_user_func_array
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 am using a user id , friend id , page and passing the values to a class so that it could detect that the login user can view the profile of other user based on other user's privacy setting. its such as after the values are passed to the class the query stuff takes place and i am using echo to tell if allow or dont means the output of class comes in these two words based on privacy setting . now the problem is how do i use this output to be used in a variable ?
i am passing the values into class like this
$error_result->check_allowed($friend_id,$id,$page);
now i know i get a output from that class as allow or dont . now how do i use that output further ?
In the function check_allowed(), after you do the queries, you need to return a TRUE/FALSE value using return statement. This could be used at the caller's side.
Example:
// ---- function definition inside the class
function check_allowed($friend_id,$id,$page)
{
// do some queries or whatever operation..
if($query_succeeded)
return true;
else
return false;
}
//----------------- outside the class, we are calling this function..
$result = $error_result->check_allowed($friend_id,$id,$page);
if($result == true)
{
echo "Allowed";
}
else
{
echo "Not allowed";
}
Hope this helps. Good luck :)