Laravel 4: load class dynamically from string in database - php

I wish i knew how to search for this question / phrase it more appropriately. This hampered my search for prior questions; bear with me if this is a duplicate.
See the update / edits at the bottom of this post
Background / what i'm trying to do:
I have a URL that looks a lot like this:
http://myapp.com/calculate/$fileID/$calculateID
$fileID and $calculateID are keys that I use to keep track of a data set and something I call a 'calculation'. Essentially, that URL says perform $calculateID on the data in $fileID.
I go to my database (mongo) and ask for a php class name or sring or file path or what have you that matches $calculateID. For example's sake let's say the table looks like this:
+-----+-------------------+
| _id | phpFile |
+-----+-------------------+
| 1 | basicCalcs.php |
| 2 | advancedCalcs.php |
| 3 | summaryCalcs.php |
+-----+-------------------+
Note: is is safe to assume that each file in the phpFile column has a common interface / set of public methods.
Example:
http://myapp.com/calculate/23/2
would go to the database, get the data from set 23 and then load up the functions in advancedCalcs.php. Once advancedCalcs.php has been loaded, a function within will receive the data. From there, a set of calculations and transformations are performed on the data.
My question
My question is what is a 'laravel 4 friendly' way to dynamically load up advancedCalcs.php and feed the data into a set of methods? Is there a way to lazy load this type of thing. Currently, i am only aware of the very unsophisticated require_once() method. I would really like to avoid this as i am convinced that laravel 4 has functionality to dynamically load an underlying class and hook it up to a common interface.
EDIT 1
Thanks to Antonio Carlos Ribeiro, i was able to make some progress.
After running dump-autoload command, my vendor/composer/autoload_classmap.php file has a few new entries that look like this:
'AnalyzeController' => $baseDir . '/app/controllers/AnalyzeController.php',
'AppName\\Calc\\CalcInterface' => $baseDir . '/app/calculators/CalcInterface.php',
'AppName\\Calc\\basicCalcs' => $baseDir . '/app/calculators/basicCalcs.php',
With code like the sample below, i can create an instance of the basicCalcs class:
$className = "AppName\\Calc\\basicCalcs";
$instance = new $className;
var_dump($instance);
Where the basicCalcs.php file looks like this:
//PATH: /app/calculators/basicCalcs.php
<?php namespace Reporter\Calc;
class basicCalcs {
public function sayHi(){
echo("hello world! i am basicCalcs");
}
};
?>
Updated question:
How can i create an alias similar to the AnalyzeController entry in autoload_classmap.php rather than having to refer to basicCalcs.php with a full namespace?

Add your library folder to your composer.json autoload:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/extended",
"app/calculators", <------- This is where you put all your calculators
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
Update your autoloaded classes:
composer dump-autoload
If you want to be sure it did worked, check if your classes are autoloaded opening the file
vendor/composer/autoload_classmap.php
To instantiate them dinamically you better have this as a table class names:
+-----+-------------------+-------------------+
| _id | phpFile | namespace |
+-----+-------------------+-------------------+
| 1 | basicCalcs | Reporter\Calc\ |
| 2 | advancedCalcs | Reporter\Calc\ |
| 3 | summaryCalcs | Reporter\Calc\ |
+-----+-------------------+-------------------+
Then you just have to use it
class CalculateController extends Controller {
public function calculate($fileID, $calculateID)
{
$file = phpFile::find($fileID);
$className = $file->namespace . $file->phpFile;
$calculator = new $className; //// <--- this thing will be autoloaded
return $calculator->calculate( $calculateID );
}
}
I'm assuming your calculators are all:
class basicCalcs {
public function calculate($calculateID)
{
return performCalculation( $calculateID ); /// something like this
}
}
And your router is somethink like
Route::get('/calculate/{fileID}/{calculateID}', array('as'=>'calculate', 'uses'=>'CalculateController#calculate'));

In Laravel4 the file composer.json is responsible for this, following is an example
{
"require": {
"laravel/framework": "4.0.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-update-cmd": "php artisan optimize"
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev"
}
Notice the autoload section where classmap is used to to tell laravel4 from which folders it should load classes and also which file should it load. For example, "app/controllers", will be used to load all classes from the app/controllers folder and "app/tests/TestCase.php" will make Laravel4 to autoload the class TestCase.php from app/tests/ folder. So, add your library folder in to the classmap section. Once you have added your folder path in autoload -> classmap section then you have to run
composer dumpautoload // or
composer dump-autoload
from the command line.

Related

Laravel organize helper functions

Please, don't talk to technical in the answers:-D I am not a hardcore programmer.
What is a good way to store certain functions in Laravel? I have functions that apply on a "post" only or "media" only, like getAttributeList or getComponents. I say "Post" and "Media" because both have their own controller, model and views. It feels wrong to put it in the model because that should be database stuff right? And traits are more for recurring functions all over the place, right? So, right now I have one big file called Helpers.php. And uh, it is getting large... should I simply separate it in PostHelpers.php, MediaHelpers.php etc? Or is there a more elegant way in Laravel to do it?
It is quite simple : Just check your composer.json file at root directory of ur app. and under autoload section add :
"autoload": {
"psr-4": {
"App\\": "app/"
},
"files": ["app/helper.php"],
"classmap": [
"database/seeds",
"database/factories"
]
"files": ["app/helper.php"], This is the line you need to add in ur composer file and provide the path to file .
In my case i have created a file helper.php in App directory where i keep all my functions .
after this run this command :
composer dump-autoload
Now u can access your functions anywhere.
In your composer json file check this snippet
"autoload": {
"files": [
"app/Helpers/global_helper.php"
],
As you see I have auto loaded 1 single file called global_helper.php in a folder called Helpers Now in this file I have a function called loadHelper(...$files)
What this function does is
if (!function_exists('loadHelper')) {
function loadHelper(...$file_names)
{
foreach ($file_names as $file) {
include_once __DIR__ . '/' . $file . '_helper.php';
}
}
}
You can pass your file name as array or string and it will include those files into your Controller constructor
So In my Controller whenever I want some helper function I create a saperate helper file for that controller then in constructor i ust include it.
I am not sure if there is any better solution but so far this is how I am making all my projects .
I hope this will help you ;)

How can I create a custom voyager Form Field from package?

I'm trying to create a repository composer package to create a custom form field for Voyager, and I found this example: https://github.com/bnku/extended-bread-form-fields , but this it doesn't work for me.
So, how do I build a custom field form for Voyager? The result would be this:
I tried this repository example.
https://github.com/bnku/extended-bread-form-fields (It didn't work for me)
and this is my repository test:
https://github.com/manuel90/crop-image-field
This is my composer.json of my package:
{
"name": "manuel90/crop-image-field",
"description": "New voyager form field to cut image when uploading",
"authors": [
{
"name": "Manuel",
"email": "testmlzra#gmail.com"
}
],
"require": {
"tcg/voyager": "^1.1"
},
"autoload": {
"psr-4": {
"Manuel90\\CropImageField\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Manuel90\\CropImageField\\CropImageFieldServiceProvider"
]
}
}
}
I can see these lines there's a trouble, it didn't detect the class "Voyager", but I don't know how to fix it:
if( class_exists('Voyager') ) {
Voyager::addFormField(CropImageFormField::class);
}
https://github.com/manuel90/crop-image-field/blob/master/src/CropImageFieldServiceProvider.php#L34-L36
( According docs this is the way to add a custom form Docs here )
I expect to see in the BREAD edit section the new custom field listed on the input type option, like this:
You need to move the Voyager::addFormField call to the boot() method as this counts as a "piece of functionality" which should be called after the voyager service providers are properly registered.
This is missing from Voyager's documentation because they only document the use case for adding FormFields at app level where the call from the register method runs after all vendor Service Providers are registered.

How can I write an accessible class in the whole of project?

I use Laravel framework and this is my current directory:
As you see, there is a class named Log (the one I've selected). Now I need to make it global. I mean I want to make it accessible in everywhere and be able to I make a object (instance) of it in following files:
All files of classe folder
All controller
web.php file of
All file of views
Anyway I want to be able to make a instande of it and call its methods everywhere like this:
$obj = new Log();
$obj->insert($message);
How can I do that?
You can create global Laravel helper:
if (! function_exists('log')) {
function log($message)
{
(new Log)->insert($message);
}
}
Put it in helpers.php and add this to composer.json to load the helpers file:
"autoload": {
....
"files": [
"app/someFolder/helpers.php"
]
},
Then you'll be able to use this helper globally:
log('User added');
In views:
{{ log('User added') }}
Update
#stack, you're using wrong syntax for JSON (screenshot in comments), here's correct one:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers/helpers.php"
]
},

Laravel5, where to put this generic code

I have some php lines which will be used very often in my application. So I would like to build a function and call it every where in my app. My function could be :
static function array_matiere($id_ecole) {
return $something;
}
I tried to put this code in a controller and then call it like that :
$liste_matieres = MatieresController::array_matieres(Session::get('id_ecole'));
It works, but where to put this kind of function ? in the controller ? in a method file ? what is the best practice ?
Thanks for your responses.
Dominique
There are multiple ways to implement this. look here
Method 1 This method is highly suggested by Laravel Expert ( JeffreyWay)
If your helper functions are similar to the way laravel 5 ones work, i.e Illuminate/Support/helpers.php you could just create a helpers.php and have composer do the autoloading for you.
Edit your composer.json file and add the file to the autoloading key. In my example my app is called Tasky so just add the files key and reference helpers.php.
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Tasky\\": "app/"
},
"files": [
"app/Support/helpers.php"
]
},
That way you just create your file inside app/Support/helpers.php and all your common function based view helpers or other helpers can go here. If you take the class based approach then the above is great.
Also then remember to just run the following once.
$ composer dumpautoload
Source
Method 2
Please Follow the link and read answer & Its comments
Whenever u need to include a piece of code providing some functionality through out your application then the best way in Laravel is to make Service class inside app/Services and then you can use that class anywhere in your application using
use App\Services\YourServiceName;

Where can I put laravel 4 filter classes?

In laravel 4, you can create filter classes instead of putting the entire filter inside a closure -- great. But do these filters have to be entirely in the app/filters.php or app/routes.php?
Generally I like to do one file per class, but I imagine there's something better to do then a bunch of includes in the filters.php file. Where would you put these for laravel to find them automatically? For example:
Route::filter('Thing', 'ThingFilter');
# can I put this in its own file and have laravel automatically use it?
class ThingFilter {
function filter() { ... }
}
I've all my filters in a separate directory called filters.
And here's how my filters.php file look like...
//---------------------------------------------------------
// Route Filters
//---------------------------------------------------------
Route::filter('auth', 'AuthFilter#default');
Route::filter('auth.basic', 'AuthFilter#basic');
Route::filter('guest', 'AuthFilter#guest');
Route::filter('csrf', 'CsrfFilter');
I autoload them via composer.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/filters",
"app/presenters",
"app/repositories",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
After you update your composer.json file, you need to run the command
composer dump-autoload
To verfiy that you files will be loaded, check out
vendor/composer/autoload_classmap.php
There isn't a default to my knowledge, but you can call ClassLoader::addDirectories(array(app_path().'/filters')); to register your filter directory. The correct place to put that is in app/start/global.php where you should see some folders already being registered.
There is a 'local.php' which seems a candidate, but this is only meant for specific environments (usually development, provided you add a proper array or closure in $app->detectEnvironment()).

Categories