How can I include php file in Lumen/Laravel AppServiceProvider register function? - laravel

When I tried to custom polymorphic types in a morph relationship, as the spec recommended,
You may register the morphMap in the boot function of your
AppServiceProvider or create a separate service provider if you wish.
I added the morpMap function in AppServiceProvider register function (I don't find the boot function in Lumen 5.3 which is used).
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$propertyConfig = include ('../config/config_property.php');
Relation::morphMap($propertyConfig['property_morph_map']);
}
}
Then, when I tried to run some php artisan commands, it threw this error,
[ErrorException]
include(../config/config_property.php): failed to open stream: No such
file or directory
but the morphMap function does work, which means the address is correct when I run this code on Homestead.
This is my first Lumen project, and I'm still not familiar with the Service Provider. It's kind of weird to me how the register function can influence the artisan command...

You can load the configure file in bootstrap/app.php through below code.
$app->configure('config_property')
To use the configure file in AppServiceProvider use configure().
Here is the code.
Relation::morphMap(configure('config_property.property_morph_map'));

Related

Class translator does not exist when using app()->setLocale() in app service provider

I'm trying to use localization in my Laravel 5.8 application. Following up with this How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application I have always do the same steps and have no problems. However, when I try it with Laravel 5.8 I keep getting Class translator does not exist from app()->setLocale(request()->segment(1)); I use that in the app service provider for some reason.
AppServiceProvider
<!-- language: php -->
public function register()
{
app()->setLocale(request()->segment(1));
Schema::defaultStringLength(191);
}
The register method is not to be used for using services.
Try adding your code in the boot method of the class.
Also I believe a better place for adding this is in a middleware then in the service provider.

Lumen/Laravel - use custom router

Is there any out of the box solution without changing core to add custom router in to laravel or lumen. I already know that lumen is using different router from laravel, so I am wondering is there any possibility builded in core to change router?
I had the same question today. After some research I found a solution which will impact the core classes minimal.
Note: The following description is based on Lumen 6.2.
Before you start; think about a proper solution, using a middleware.
Because of the nature of this framework, there is no way to use a custom Router without extending core classes and modifying the bootstrap.
Follow these steps to your custom Router:
Hacky solution
1. Create your custom Router.
Hint: In this example App will be the root namespace of the Lumen project.
<?php
namespace App\Routing;
class Router extends \Laravel\Lumen\Routing\Router
{
public function __construct($app)
{
dd('This is my custom router!');
parent::__construct($app);
}
}
There is no Interface or similar, so you have to extend the existing Router. In this case, just a constructor containing a dd() to demonstrate, if the new Routerist going to be used.
2. Extend the Application
The regular Router will be initialized without any bindings or depentency injection in a method call inside of Application::__construct. Therefore you cannot overwirte the class binding for it. We have to modify this initialization proccess. Luckily Lumen is using a method just for the router initialization.
<?php
namespace App;
use App\Routing\Router;
class Application extends \Laravel\Lumen\Application
{
public function bootstrapRouter()
{
$this->router = new Router($this);
}
}
3. Tell Lumen to use our Application
The instance of Applicationis created relativley close at the top of our bootstrap/app.php.
Find the code block looking like
$app = new Laravel\Lumen\Application(
dirname(__DIR__)
);
and change it to
$app = new App\Application(
dirname(__DIR__)
);
Proper solution
The $router property of Application is a public property. You can simply assign an instance of your custom Router to it.
After the instantiation of Application in your bootstrap/app.php place a
$app->router = new \App\Routing\Router;
done.

How to integrate in Laravel 5 a Package from Packagist?

I'm working with laravel 5 and trying to integrate the following package:
exacttarget/fuel-sdk-php
I executed on my project:
composer require exacttarget/fuel-sdk-php
So I had on my vendor dir exacttarget provider.
First thing I've noticed this particular package doesn't use namespaces, so it still calls require directives but not "use \path\namespace"
Is it a right approach? I haven't seen many packages yet but among my past experience doesn't look to me the right approach to write a package...
After this I edit condif/app.php to use ET_Client class.
'providers' => [
...
'ET_Client',
...
],
Once I did this, I got an error: looks like Laravel frmwk tries to instantiate the class, that needs some parameters to work, even if I'm not yet using it (istantiating). It this a normal behavior from Laravel?
Am I missing something ?
The providers array is for registering service provider classes. Unless ET_Client extends Laravel’s base ServiceProvider class, it’s not going to work.
Instead, just add the use statements to your PHP classes as and when you need to use the class:
<?php
namespace App\Http\Controllers;
use ET_Client;
class SomeController extends Controller
{
public function someAction()
{
// Instantiate client class
$client = new ET_Client;
// Now do something with it...
}
}

Confused - AppServiceProvider.php versus app.php

Where do I specify my bindings exactly? It seems I can do it in either of these files.
config/app.php
Inside of 'providers' =>
app/Providers/AppServiceProvider.php
Inside of register()
The service providers array is loaded via config/app.php. This is the only actual place that providers are registered, and is where you should put Service Providers.
AppServiceProvider is for Laravel-specific services that you've overridden (or actually specified), such as the Illuminate\Contracts\Auth\Registrar, The HTTP/Console Kernels, and anything you wish to override in Laravel. This is a single service provider, that registers container bindings that you specify.
Really, you could load anything you want here, but there's a bunch of ready-to-go service providers in the app/Providers directory for you so you don't have to go and make one yourself.
If your bindings are not related to App, then I would create a new ServiceProvider class where I overwrite the register method with my new binding, then you have to let Laravel know that this class exists registering as a Provider in your config/app.php providers list, that is:
app/Providers/MyNewClassServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MyNewClassServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind(
'App\Repository\MyNewClassInterface',
'App\Repository\MyNewClassRepository'
);
}
}
config/app.php
'providers' => [
// Other Service Providers
'App\Providers\MyNewClassServiceProvider',
],

How to make Laravel migrator work with namespaced migrations?

I namespaced my whole package successfully but I can not get namespaced migrations to work. In the autoload_classmap.php the migration classes are nicely namespaced, but the Migrator is not looking for the migration classes within the namespace. How to get the migrator to search for the migrations within the namespace?
The migration file
<?php namespace Atomend\Aeuser;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Schema, User;
class UsersTable extends Migration {
public function up() {
Schema::create("users", function(Blueprint $table) {
$table
->increments("id");
autoload_classmap.php
'Atomend\\Aeuser\\UsersTable' => $baseDir . '/src/migrations/2014_04_21_184359_users_table.php',
Terminal error
PHP Fatal error: Class 'UsersTable' not found in
This is logical since the UsersTable is in the Atomend\Aeuser namespace.
Issuing the migration
php artisan migrate --bench="atomend/aeuser"`
So to be clear, when losing the namespace everything works fine and dandy.
Laravel migrator doesn't play nice with namespaced migrations. Your best bet in this case is to subclass and substitute the Migrator class, like Christopher Pitt explains in his blog post: https://medium.com/laravel-4/6e75f99cdb0.
add "Database\Migrations\": "database/migrations/" to composer.json -> autoload:psr-4

Resources