Package macros in Laravel 4 - laravel-4

Where is the best place to put a Form Macro in a Laravel 4 package? Looking through the package documentation, I don't see an obvious place. Seems like it won't fit anywhere under /src. I've only been learning Laravel 4 for 2 weeks, so I'm pretty new at this.
Thanks for and advice.

You will struggle to access the Form facade if you directly autoload that file from composer.json. A better solution is to add your macros in app/macros.php and then add the following line in app/start/globals.php:
require app_path().'/macros.php';
Documented under Start Files here: http://laravel.com/docs/lifecycle

Doesn't have an explicit place, just like on app level form macros doesn't have a dedicated file to place them in.
Just make sure you load the file where you register the macro.
You could even put it inside the ServiceProvider if there is only a single macro we are talking about.
Or autoload from composer.json.
{
"autoload": {
"files": [
"path/to/macros.php"
]
}
}

Related

How to use a vendor class in Laravel?

I would like to use this package https://github.com/FineUploader/php-traditional-server/ in my Laravel project. So I modified composer.json file like that:
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.2.*",
"fineuploader/php-traditional-server": "1.0.0"
}
And run a compose update command. But how can I use UploadHandler class (declared in handler.php of the package) on my Controller?
The package isn't using namespaces or much else the Composer autoload could provide magically for you, so you could either include the class file yourself:
include "vendor/FineUploader/php-traditional-server/handler.php";
or as you are using the Composer autoloader in Laravel, you should define a classmap.
In your composer json:
{
"autoload": {
"classmap": ["vendor/FineUploader/php-traditional-server"]
}
}
Then run composer dump-autoload
(including "vendor/autoload.php" is already provided by the Laravel boot).
All the classes in their library will be auto-loaded at the moment your code requires them.
Update: why classmap is the most suitable method.
Autoloading is the most efficient and performant way to load a class, because it is only parsed, compiled, loaded into memory when the class is actually needed. "Classmap" autoloading is the intended mechanism to load classes which are not in a namespace, or are not in PSR0 or 4 format.
Conversely, using a global (app boot) include set for classes, or similarly using the "files" option in the composer autoloader would be the most inefficient as it would load all dependencies for all application requests, regardless of whether they are going to be used or not. In fact using autoload "files" for class dependencies is worse than the traditional approach of having specific includes in each of your scripts. At least then you would only pre-loading the specific requirements, thus similar to autoload in memory usage.
The "files" autoload option is intended for flat libraries of functions (not OO), which are simply pre-loaded on all requests, because there is no auto-load mechanism for function calls.
Note the use of op-code caches would soften the efficiency difference between the two, but the memory usage issue remains.
#scipilot answer should work too, but I will give my own choice:
Add this part to Your composer.json file and run composer update.
"files": [
"vendor/fineuploader/php-traditional-server/handler.php"
]
After this, class will be visible in project scope and Your script will work.
More about auto loading files in composer here: https://getcomposer.org/doc/04-schema.md#files
Did you try use UploadHandler directly?
Composer have autoloading feature, so it can pick up your "dependencies" on the fly

Whats the ideal way to store a bunch of independent constants in a laravel project?

I thought of creating a constants.php file in the config directory but didn't go ahead with it as it kinda contradicts the idea of "CONSTANTS" as anything in config can be changed at run time. So what are the best practices in storing constants in a laravel project without too many "require_once"s or "use"s ?
pointer to some public git project which uses a standard way to get this done would be great.
you have to make constant in one file and use all over laravel than you have to make one file that can we write all the constant and costume functions.
Follow the steps
Make one Helper.php file in the your app->Http directory.
Write the all the constant and function there.
Then make following changes in your composer.josn
"files": [
"app/Http/helpers.php"
],
Then following command in your laravel project directory.
$ composer dumpautoload

Best practice to modular programming in Laravel 5+

I'm starting a new project and I want to reuse some parts of it, mainly the stuff related to user registration and authentication. I can copy and paste all of the code but I want to use again. I know there is Package Development in Laravel but it's not easy and feel like there must be a better way.
Some days ago I find a pingpong/modules but I don't know about it. It's third party plugin and don't trust it.
Use this plugin is true? Is this plugin is updated later? What's different between Embedd Package Laravel and pingpong/modules? or Do you have any suggestion?
Pingpong modules seems to be build for the earlier version of Laravel 5 and in how far they are compatible with future versions (and maybe current 5.1.11) I cannot say.
There isn't much activity going look the commit history for 2.1, as of today(18 dec) the last commit was over 6 months ago.
But is the package specifically designed for Laravel? It seems to. They offer a bunch of features which are useful for development. The only unfortunate thing is you get a LOT of code within your own git environment (is it a good thing? I don't know, what do you prefer).
Personally I don't like it in this way for development, I prefer them in the vendor/ folder else it's a pain to update it to newer a version.
Since Laravel 5 Taylor wanted to make package development not too specific anymore, like in Laravel 4. The only thing what you can do (but not have to) to make your package using Laravel is using the ServiceProvider's. The ServiceProvider is the bootstrap into the Laravel application.
If you want to extend or implement your own functionality, fork the repo and build it yourself on top off it and host it (through github/packagist or a private repo using Satis).
Pingpong modules (2.1) is build for Laravel 5 and they you described (Embedded Laravel Package) is more for Laravel 4, because the more specific way you have to write the package.
But, there is alternative?
Whenever you want a more active project/package for development you should tryout Asgard CMS. They are pretty modular and I thought I read somewhere it was inspired by this package (totally not sure).
How about building yourself?
Of course you can build your own packages to achieve the same result. And create it as modular as you want. I created a lot modules for my company and we can create pretty easy a entire system and using and extending/overriding modules. Even small parts from a module can be overwritten to project specific needs.
We have chosen for almost the same structure as the app/ folder which Laravel projects, in case of CMS/API modules.
A packages look like:
tests/
src/
Acme/
Controllers/
Requests/
Models/
Module.php // contains some specifc calculations for example
ModelServiceProvider.php
composer.json
In the composer.json file we autoload: "Module\\": "src/"
And in the config/app.php we register the ModuleServiceProvider. Now we injected the functionality into Laravel's container and can we use it through the app() instance.
But whenever we only want to use the Models with in another project or standalone, we can still use it because the autoloaded features from composer and the way we build the package. Possible to use:
<?php
require_once __DIR__ .'/vendor/autoload.php';
use Module\Models\Module;
$module = new Module;
Edit
The package structure we like to use, to have a section for API or CMS stuff:
tests/
src/
Cms/
Controllers/
Requests/
Api/
Controllers/
Transformers/
Models/
Module.php // contains some specifc calculations for example
Providers/
CmsServiceProvider.php // includes `ModuleServiceProvider`
ApiServiceProvider.php // includes `ModuleServiceProvider`
ModuleServiceProvider.php // contains global stuff like commands etc.
composer.json
and instead of registering ModuleServiceProvider in config/app.php we register the ApiServiceProvider or CmsServiceProvider depending on the wishes of the client/project.
To reuse your classes simply use php namespaces or use to call back your clases.
Using the namespace
namespace Acme\Tools;
class Foo
{
echo "me";
}
You can the call class foo
<?php
$foo = new \Acme\Tools\Foo();
Using Use.
You can also use use Statement as below :
<?php
use \Acme\Tools\Foo;
$foo = new Foo();
Use Middleware
You should also use middleware to filter who should use the scripts ie the Auth middle-ware , which will help you in filtering users , registrations , logins READ MORE http://laravel.com/docs/5.1/middleware
Use Eloquent
Use ORM to create REST apis to your models , its very simple , always let your controller class extend eloquent use Illuminate\Database\Eloquent\Model; ie as :
use Illuminate\Database\Eloquent\Model; .Read More http://laravel.com/docs/5.1/eloquent
Lastly Use Laravel In built Helper functions
There are numerous Laravel In built Helper functions , to use simply go over the documentation to help you
I've used pingpong modules. It a pretty cool package. I'm not sure if it's updated much. But it's a very simple package. The only thing it does is create a folder with almost the same structure as in the app folder + views. But these are modules. You can reuse it if you program them right. The same goes for the other answer from jimmy if you have a good structure you can reuse anything.
EDIT
In the image below you'll see an example of pingpong modules. As you it's pretty much the same structure as the app folder. Maybe more the root folder. Normally it runs start.php and you have a routes.php file int he Http folder. I customized mine a bit. And load the frontend and backend routes within the RouteServiceProvider. This is build with laravel 5.1.

Laravel 5 namespaces

I just downloaded Laravel 5 and started migrating to it. However, I find the required use of namespaces really annoying.
I don't feel like I am getting much from it, other than cluttering my code.
How can I disable the namespacing requirement?
I don't think you should disable or remove namespaces. The main reason for namespacing is to avoid conflicts with classes that have the same name. As soon as an application gets larger you will have classes that have the same name. Example from the Framework source:
Illuminate\Console\Application and Illuminate\Foundation\Application
Both are called the same. Only because of the namespacing you can import the right class. Of course you could also name them:
ConsoleApplication and FoundationApplication
But while the namespace normally is only used when importing a class at the top of a file:
use `Illuminate\Console\Application`
The name itself is used everywhere in the code. That's something that really clutters up your code, too long class names.
Besides the naming thing, namespaces also encourage better structure and help with knowing where your files are. That's because Laravel's default structure is PSR-4 compliant. That means if you have a controller App\Http\Controllers\HomeController you can be certain that you will find a HomeController.php under app/Http/Controllers.
I am aware of that, but it's not needed in the project I am working on.
Maybe it doesn't make sense for the current project but getting used to namespaces will help you tackle bigger projects in the future
And being a Sublime Text user, which doesn't have autoimport, it really gets to be a pain
I don't know Sublime Text that well, but CodeIntel might have auto import. Otherwise consider switching to another editor / IDE. I can highly recommend JetBrains PhpStorm
In the end, if you still don't want to use namespaces, keep using Laravel 4 or search for another framework that follows less good practices...
Removing namespaces from your app classes
While a totally don't recommend this, it is possible to at least remove some of the namespacing in your application.
For example the default controller namespace App\Http\Controllers can be changed to no namespace at all in RouteServiceProvider:
protected $namespace = '';
And for your models you can just remove the namespace in the file and your good. But keep in mind that without namespaces PSR-4 autoloading won't work anymore. You will have to autoload your files using classmap in composer.json
You can avoid using namespaces for own classes by defining them in the global namespace in your composer.json file. Like this:
"autoload": {
"psr-0": {
"": ["app/Http/Controllers/",
"app/models/",
"app/helpers"
]
},
You will also have to change your app/Providers/RouteServiceProvider.php to:
protected $namespace = '';
for routing to work.

Using php scripts on my views in Laravel 4

I am using the Laravel 4 framework, and I am trying to set up the Facebook authentication system. I have an authentication system I had set up on another site (not using a framework) that used a config.php and process_facebook.php file. I am trying to implement this config.php file into my views. So far, I am including the files in a folder called "includes", within my "app" folder. I am trying to use the following code to implement it:
$app = app();
include($app['path.app'].'/includes/config.php');
My question is, where in the view do I put this code? Do I have to use php tags? (I am using the blad functionality). Your help is appreciated.
Laravel is an MVC framework, the purpose is to organise your code and clean your views. So this shouldn't be in your view.
I think the best way should be :
Create a facebook.php file in the config folder wich contains all your facebook configuration (read http://laravel.com/docs/configuration)
Create a folder named services, helpers or includes (as you want) and put process_facebook.php inside (I bet it contains the methods to deal with facebook API).
Add two lines of configuration to include this new folder
Like that :
// composer.json
"autoload": {
"classmap": [
[...]
"app/services",
]
},
// start/global.php
ClassLoader::addDirectories(array(
[...]
app_path().'/services',
));
Then, you can use your facebook class or methods all over your app.
The route you are taking to include configuration files is not recommended, but it is possible. Please see information about Laravel Configuration Files.
You should be able to use the following in your view:
<?php include(app_path().'/includes/config.php'); ?>
As it is a configuration file, it would be better to use require() instead of include().
In addition, it would also be better to include the file in the necessary controller(s).

Resources