So I have a public directory in my localhost that contains some classes that I want to be able to use in my current laravel project.
So how will I include these, external classes/files in laravel?
I tried to add these files in my composer.json file,
"autoload": {
"classmap": [
"database",
"../public_classes/priceClass.php"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"../public_classes/priceClass.php"
]
},
and do composer dump-autoload, then when I tried to call the function, it won't work.
You don't need to add anything in composer.json if you're using PSR-4. For example, you can create PHP file in \app\MyClasses\MyClass.php and use this namespace in custom class: namespace App\MyClasses.
Laravel will autoload this class for you.
I also wanted to include a few classes in my Laravel Project for that I just copied the files in a new directory(app/Libraries) and assigned Package name to all the classes and followed PSR-4 Autoloading standard for all those class files and I have added following code in my composer.json.
...
"autoload": {
"classmap": [
"app/Libraries",
],
...
hope this will solve your problem...
Happy to help!!!
What I end up doing was I put the path to file in my composer.json, then do composer dump-autoload.
composer.json:
"autoload": {
"classmap": [
"database",
"../public_classes/priceClass.php"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"../public_classes/priceClass.php"
]
},
then, the file I wanted to add, I edit it into simple functions instead of oop ones.
priceClass.php:
<?php
function getPrices(){
/*
*
*
*Statements here
*
*/
}
?>
And call it in my laravel controller simply by
$prices = getPrices();
I did this, because when I tried to have something like:
class Prices(){
public function getPrices(){
....
}
}
then call it in my laravel controller:
$priceClass = new Prices();
$prices = $priceClass->getPrices();
I ended up having an error, something like: Prices class not found.
Related
I can see my alias present in app aliases array:
dd(app()); // ... "Me\Package\Facades\PHPFile" => "PHPFile"
using that same full name works
dd(
\Me\Package\Facades\PHPFile::load('app/User.php')
);
BUT trying to use the registered alias:
dd(
\PHPFile::load('app/User.php')
);
// Error: Class 'PHPFile' not found
Even though it was registered in app()! Any ideas whats going on here?
In my package composer.json:
"extra": {
"laravel": {
"dont-discover": [],
"providers": [
"Me\\Package\\MyServiceProvider"
],
"aliases": {
"PHPFile": "Me\\Package\\Facades\\PHPFile"
}
}
},
Also tried adding this in my serviceproviders register method:
$this->app->alias('PHPFile','Me\Package\Facades\PHPFile');
Host application composer.json
add
"repositories": [
{
"type": "path",
"url": "/full/path/to/packages/Me/Package"
}
],
Then install with composer require "me/package #dev"
Thanks to #lagbox for spotting my error in comments
MediaWiki's merge-plugin requires a data structure as follows:
{
"extra": {
"merge-plugin": {
"include": [
"extensions/OpenIDConnect/composer.json"
]
}
}
}
How can this data structure (note the array) be created via CLI?
config extra.merge-plugin.include ["extensions/OpenIDConnect/composer.json"]
will not create the desired output
With Composer 2, this can be accomplished by passing a flag such as --json:
For the example above (note the single quotes parameterizing the JSON object:
composer config --json extra.merge-plugin.include '["extensions/OpenIDConnect/composer.json"]'
Results in:
"merge-plugin": {
"include": ["extensions/OpenIDConnect/composer.json"]
}
Reference: https://github.com/composer/composer/pull/8779
I must hash database values before add to db and rehash after select for human friendly readable. Now I create "Helper\Helpers.php" in app folder. In the helpers file I have functions for encrypt/decrypt values. Helper file registered to composer autoloads:
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files":[
"app/Helpers/Helpers.php"
]
},
In my opinion this method (file registration in the composer) is an unprotected option for registering a file of a function stored in itself for encryption. For more reliable protection where can I store and use files with encryption and decryption functions?
If you have a model defined, I would suggest Accessors and Mutators. It would allow you to encrypt the value before written to storage and then decrypt when read out:
// mutator
public function setMyHashedValueAttribute($value)
{
$this->attributes['my_hashed_value'] = encrypt($value);
}
// accessor
public function getMyHashedValueAttribute($value)
{
return decrypt($value);
}
I have a file called \App\Helpers, which contains a couple of functions one it to check if the user is an admin
#if(\Helper::isAdmin())
do something
#endif
I could add a method to the user model and get
$user->isAdmin()
This is a bit neater. However, is there any way just to do
#if(isAdmin())
do something
#endif
Yes, you can do this by creating a file with some global functions in it, so for example, add an entry in your composer.json file like one given below:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"helpers/functions.php"
]
},
Notice the files section. then create a top level directory inside your project root as helpers and create the functions.php file inside it and declare your global functions, for example:
// helpers/functions.php
if (! function_exists('isAdmin')) {
function isAdmin()
{
// You can entirely rewrite the logic here or
// you can use your existing Helper::isAdmin()
return \Helper::isAdmin();
}
}
Don't forget to run composer dump-autoload at last. Btw, if you are in Larave 5.5.x then you may use larave'ls new Blade::if() Directives described here.
I want to have a little string converter (translator) helper method. For example, I want to pass my string from the controller and get the result, so I don't want to repeatedly write the same helper function into the controller.
So that I can maybe use it like:
$oldStr = '12 Mayıs 2014'; // which means 12 May in English
$englishStr = custom_date_translator($oldStr);
and it does it's thing on the helper method.
Is the Service Providers for this purpose? It'd be good to learn the right way to do it.
You can create a custom helpers.php file and setup Composer so that it's autoloaded.
For example, if you create the helpers.php file in the app/ directory, you could edit your composer.json file to something like this :
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php"
]
},
Make sure to run composer dump-autoload in your console to update the autoloader.