I am trying to create a global helper to store site settings in my Laravel application. Meaning, I need a helper to set settings from a controller and access that settings object from anywhere using the same helper. I don't need to store these settings on a database or in files. That's why I need these kinds of functionality.
What is did are as follows,
Create a class in app/Helpers directory called SettingsHelper and auto load the directory.
namespace App\Helpers;
class SettingsHelper{
protected $vars;
public function all(){
return $this->vars;
}
public function get($key, $default = null){
if (is_array($this->vars) && array_key_exists($key, $this->vars)) {
return $this->vars[$key];
}
return $default;
}
public function put($key, $value){
return $this->vars[$key] =$value;
}
}
Create a helper function to resolve the class if not already resolved
if ( ! function_exists('settings')) {
function settings(){
if (app('\App\Helpers\SettingsHelper')) {
return app('\App\Helpers\SettingsHelper');
}
return app()->make('\App\Helpers\SettingsHelper');
}
}
Set and get setting using the helper
settings()->put('test', 'test2');
dd(settings()->all());
But null is returned. I tried dumping settings()->put('test', 'test2') and it returned the passed value test2.
Is this the correct approach for this?
You should register your class as a singleton in the application container before you can use it. Otherwise, the app() function will create a new instance on every use. You can also give the instance a name in the container, this way, you don't have to create your own global function.
In your AppServiceProvider.php:
$this->app->singleton('settings', function ($app) {
return new \App\Helpers\SettingsHelper;
});
And now you can use your class with:
app('settings')->put('test', 'test2');
dd(app('settings')->all());
Create a service provider php artisan make:provider HelperServiceProvider
Then in the register method do something like:
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename) {
require_once($filename);
}
}
Make a singleton in a provider, example AppServiceProvider.php.
use App\Helpers\SettingsHelper;
$this->app->singleton(SettingsHelper::class, function ($app) {
return new SettingsHelper($vars); // create your class with the vars you need
});
Everywhere in your app there can only be 1 settings helper and you can get it like so.
use App\Helpers\SettingsHelper;
app(SettingsHelper::class);
Related
I am using laravel framework to develop API's, i am using laravel-saptie audit package to monitor all the users activity.now i have to modify or add the some functionality in LogsActivity Trait boot method ,after some research i am using like following
LogsActivity.php
trait LogsActivity{
protected static function bootLogsActivity(): void
{
// Hook into eloquent events that only specified in $eventToBeRecorded array,
// checking for "updated" event hook explicitly to temporary hold original
// attributes on the model as we'll need them later to compare against.
static::eventsToBeRecorded()->each(function ($eventName) {
if ($eventName === 'updated') {
static::updating(function (Model $model) {
$oldValues = (new static())->setRawAttributes($model->getRawOriginal());
$model->oldAttributes = static::logChanges($oldValues);
});
}
static::$eventName(function (Model $model) use ($eventName) {
$model->activitylogOptions = $model->getActivitylogOptions();
if (! $model->shouldLogEvent($eventName)) {
return;
}
$changes = $model->attributeValuesToBeLogged($eventName);
$description = $model->getDescriptionForEvent($eventName);
$logName = $model->getLogNameToUse();
// Submitting empty description will cause place holder replacer to fail.
if ($description == '') {
return;
}
if ($model->isLogEmpty($changes) && ! $model->activitylogOptions->submitEmptyLogs) {
return;
}
// User can define a custom pipelines to mutate, add or remove from changes
// each pipe receives the event carrier bag with changes and the model in
// question every pipe should manipulate new and old attributes.
$event = app(Pipeline::class)
->send(new EventLogBag($eventName, $model, $changes, $model->activitylogOptions))
->through(static::$changesPipes)
->thenReturn();
// Actual logging
$logger = app(ActivityLogger::class)
->useLog($logName)
->event($eventName)
->performedOn($model)
->withProperties($event->changes);
if (method_exists($model, 'tapActivity')) {
$logger->tap([$model, 'tapActivity'], $eventName);
}
$logger->log($description);
// Reset log options so the model can be serialized.
$model->activitylogOptions = null;
});
});
}
}
<?php
namespace App\Http\Traits;
use ReflectionMethod;
use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Traits\LogsActivity;
trait CustomLogTrait{
use LogsActivity
{
LogsActivity::bootLogsActivity as parentbootLogsActivity;
}
protected static $logOnlyDirty = true;
public static function bootLogsActivity(){
$this->parentbootLogsActivity;
static::creating(function(Activity $model){
$act = $model->all()->last();
$act;
});
}
}
i am facing this problem Using $this when not in object context {"exception":"[object] (Error(code: 0): Using $this when not in object context.instead of resolving this one if i use directly in customTrait inside bootLogsActivity() function like this
LogsActivity::bootLogsActivity
still this one also throwing an error like protected one can't able to access.can anyone help to me override the LogsActivity boot method inside my customLogTrait ?
Thanks in Advance !
You are trying to access this from a static context.
Thus, the line:
$this->parentbootLogsActivity;
Shall be modified to:
self::parentbootLogsActivity;
I am new to Laravel. I have some functions in PaymentController. I want to call those functions from SmartpaySController. Here is the function which is available in PaymentController. Help me to call that function by staying in SmartpaySController.
public function getPaymentFailed($paymentId) {
$transactionData = $this->paymentRepo->find($paymentId);
if($transactionData) {
$data['quote'] = $this->quoteRepo->getQuoteById($transactionData->quote_id);
$data['metaTitle'] = 'Payment failed';
$data['returnMessage'] = $transactionData->return_message;
return view('payment::payment.quote_payment_failed', $data);
}
}
Thank you.
Instead of calling controller methods, the better practice is that you can create traits like: app/Traits and extend in controller
//trait
trait traitName {
public function getData() {
// .....
}
}
//Controller
class ControlelrName extends Controller {
use TraitName;
}
I recomend you to not call functions from one controller to another.
Make Helpers, Resources or implement same feature in other way
Never use controllers as object
But if you want to do it anyway you can use:
SomeController.php
class SomeController extend Controller {
public function someFunction(Request $request) {
// Here Some Code
}
}
YourController.php
use SomeController;
...
public function getPaymentFailed(Request $request, $paymentId) {
$controller_data = (new SomeController)->someFunction($request);
$transactionData = $this->paymentRepo->find($paymentId);
if($transactionData) {
$data['quote'] = $this->quoteRepo->getQuoteById($transactionData->quote_id);
$data['metaTitle'] = 'Payment failed';
$data['returnMessage'] = $transactionData->return_message;
return view('payment::payment.quote_payment_failed', $data);
}
}
Change:
public function getPaymentFailed($paymentId)
to:
public static function getPaymentFailed($paymentId)
This will make it staticly available in your SmartpaySController by doing:
PaymentController::getPaymentFailed($paymentId);
You can make use of Real-Time Facades
Using real-time facades, you may treat any class in your application
as if it were a facade.
To generate a real-time facade, prefix the namespace of the imported
class with Facades:
//...
use use Facades\App\Http\Controllers\SomeController;
//...
return SomeController::getPaymentFailed($request, $paymentId);
I'm trying to add a custom assertion to the TestReponse class so I can make something like this:
$response = $this->json('POST', '/foo/bar');
$response->myCustomAssertion();
I tried creating an App\TestResponse class that extends the original one and then binding it in the App\Provider\AppServiceProvider class.
public function register()
{
$this->app->bind('Illuminate\Foundation\Testing\TestResponse', function ($app) {
return new App\TestResponse();
});
}
But $response->json() is still returning the original one and not my own implementation.
How can I extend the TestResponse class?
If you want a little more fine-grained control, you can also extend the Illuminate\Foundation\Testing\TestResponse, as you have done, and then override the createTestResponse method in your TestCase class to return an instance of your custom response class:
// Laravel 8 and above
protected function createTestResponse($response)
{
return tap(App\TestResponse::fromBaseResponse($response), function ($response) {
$response->withExceptions(
$this->app->bound(LoggedExceptionCollection::class)
? $this->app->make(LoggedExceptionCollection::class)
: new LoggedExceptionCollection
);
});
}
// Before Laravel 8
protected function createTestResponse($response)
{
return App\TestResponse::fromBaseResponse($response);
}
From Illuminate\Foundation\Testing\Concerns\MakesHttpRequests.
The TestResponse class uses the Macroable trait so you can add macro functions at runtime.
TestResponse::macro('nameOfFunction', function (...) {
...
});
You can add this to a Service Provider's boot method or somewhere before you need to make the call to that macro'ed method.
I have separate validate_date functions in controller1.php, controller2.php, and controller3.php. I want to create a global function in DateTimeHelper.php to validate dates instead of creating a function in each controller for this functionality. I am using Laravel in the backend
Controller1:
private function validate_date($date)
{
$split_date = explode('/', $date);
if (count($split_date) == 3) {
return checkdate($split_date [0], $split_date [1], $split_date [2]);
}
return false;
}
Controller2 && controller 3:
private function validate_date($date)
{
if ($this->check_field($date) && $date !== 'Invalid date') {
return true;
}
return false;
}
My problem is that I am using different functions in different controllers, I need a global function to validate dates instead of creating a function in each controller for this functionality.
The following method will probably do what you're looking for:
Create a new file called DateTimeHelper.php inside a (new) Helpers folder. The final path will be app\Helpers\DateTimeHelper.php.
The content of DateTimeHelper.php will look like:
<?php
namespace App\Helpers;
class DateTimeHelper {
public function validate()
{
}
public function checkField()
{
}
}
Now edit the controllers where you want to use the class, add a procted property $dateTimeHelper and use Laravel's Automatic Injection like this:
<?php
namespace App\Http\Controllers;
use App\Helpers\DateTimeHelper;
class UserController extends Controller
{
protected $dateTimeHelper;
/**
* Create a new controller instance.
*
* #param DateTimeHelper $dateTimeHelper
* #return void
*/
public function __construct(DateTimeHelper $dateTimeHelper)
{
$this->dateTimeHelper = $dateTimeHelper;
}
public function show($date)
{
return $this->dateTimeHelper->validate($date);
}
}
Hopefully this should solve the problem, however, I'd recommend to look into Laravel validation.
How about a Service Class instead of helper function and put all possible date validation from the system into this Service Class?
I'm building an application, now i'm created a helper
class Students{
public static function return_student_names()
{
$_only_student_first_name = array('a','b','c');
return $_only_student_first_name;
}
}
now i'm unable to do something like this in controller
namespace App\Http\Controllers;
class WelcomeController extends Controller
{
public function index()
{
return view('student/homepage');
}
public function StudentData($first_name = null)
{
/* ********** unable to perform this action *********/
$students = Student::return_student_names();
/* ********** unable to perform this action *********/
}
}
this is my helper service provider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelperServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
foreach(glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
}
i event added it as an alias in config/app.php file
'Student' => App\Helpers\Students::class,
Try putting use App\Helpers\Student; at the top of your controller beneath the namespace delcaration:
namespace App\Http\Controllers;
use App\Helpers\Student;
class WelcomeController extends Controller
{
// ...
Look more into PHP namespaces and how they are used, I believe you may have a deficient understanding about them. Their only purpose is to make so you can name and use two classes with the same name (e.g. App\Helpers\Student vs maybe App\Models\Student). If you needed to use both of those classes inside of the same source file, you can alias one of them like this:
use App\Helpers\Student;
use App\Models\Student as StudentModel;
// Will create an instance of App\Helpers\Student
$student = new Student();
// Will create an instance of App\Models\Student
$student2 = new StudentModel();
You do not need to have a service provider for this, just the normal language features. What you would need a service provider for is if you wanted to defer the construction of your Student object to the IoC:
public function register()
{
$app->bind('App\Helpers\Student', function() {
return new \App\Helpers\Student;
});
}
// ...
$student = app()->make('App\Helpers\Student');
You should never have to include or require a class file in laravel because that is one of the functions that composer provides.
You do not need a service provider to make it works. Just lets the Students class as you did:
class Students{
public static function return_student_names()
{
$_only_student_first_name = array('a','b','c');
return $_only_student_first_name;
}
}
all its methods should be static
You added the Facade correctly:
'Student' => App\Helpers\Students::class,
Finally, looks like your problem is caused by forgetting a backslash at facade name. Uses \Students instead of Students:
public function StudentData($first_name = null)
{
$students = \Student::return_student_names();
}
When using a facade, it is not necessary makes nay include, the facades were made to avoid complex includes in everywhere.