Lumen : use config constant file from other folder - php

I have laravel and lumen running parallel on a server.
and structure is like
var/www/application/laravel
var/www/application/lumen
Now laravel app is already developed for full fledged website. And lumen folder is being used for webservices. Now there are many constant files in config folder like config>messages.php and I want to reuse those without rewriting it in lumen. But currently all I can see is I can access only lumen constants in lumen, cant access laravel constant file.
Such as config('messages.status'); can only access value when messages.php is in lumen.
Any idea how do I configure lumen code to include laravel config constants file too?
p.s. lumen version : 5.4, laravel version : 5.2
Thanks.

You can load all the Laravel app configs in your Lumen by making a new config file and requiring all files like this:
/var/www/application/lumen/config/custom.php
<?php
$path = '../../laravel/app/config/';
$config = [];
foreach ( scandir($path) as $filename ) {
$filePath = $path . '/' . $filename;
if (is_file($filePath)) {
$config += require_once $filePath;
}
}
return $config;
And then you can access them in your Lumen like this config('custom.key');.

That's right, there are predefined paths for configuration files. Take a look at vendor/laravel/lumen-framework/src/Application.php. There's a getConfigurationPath method there.
Basically, Lumen will try to load config files from your config folder, and if nothing is found then will load from /vendor/laravel/lumen-framework/config/*
I suggest you symlink them. As I don't see any way to modify Lumen core for this (actually, yes, you could override Application class and instantiate it in the bootstrap, but you should take care of things like the __DIR__ constant inside it).
UPDATE
Maybe it's easier. Take a look at this loop: Lumen: print custom config return NULL . The guy is loading every config file in a loop using $app->configure($file); method.
Actually, looks like the TheFallen solution here.

Related

Access Core PHP folder inside a Laravel 8 project

Laravel Project is installed in my root directory and I have another project inside root directory with Core PHP ,
COREPHP-PROJECT is the one I want to access , but every time I try to access example.com/COREPHP-PROJECT it's giving me 404 error.
I have tried more than 50 solutions from internet and nothing is working so far.
What I'm doing wrong here?
NOTE: .htaccess file is renamed as I wanted to test without it too.
I think it is actually fine to have a project directory inside the applications root directory. A good application should not be build inside the framework. It should be build with the framework. If in 5 years somehow laravel is not anymore supported you can decouple it more easy and use it in a new framework.
You just need to assign the path to your composer.json under the psr-4 part.
"autoload": {
"psr-4": {
"MainApplication\\": "app/",
"CorePhpProject\\": "CORE-PHP-PROJECT/app/",
But you should only route over the Laravel router.
You can add your routes inside laravels RouteServiceProvider.php
Have a look at the documentation:
https://laravel.com/docs/8.x/routing#the-default-route-files
Should look something like this then:
Route::prefix('coreproject')
->group(base_path('CORE-PHP-PROJECT/routes/web.php'));
Then you can call your laravel routes like this
/home
and the project one are called like /coreproject/home
I got into same task of integrating core php folder inside laravel , therefore solution i found that create a core php file with extension test.php in public folder at laravel root, and try to access in browser after php artisan serve, you will find you can access it.

Overriding configuration files of composer packages in Lumen

I have a Lumen project with external Composer packages installed. As usual with Lumen, they're stored in the vendor directory, each in their respective folder. Some of these packages have configuration files, which I would like to override with custom ones.
I have registered the files in my bootstrap/app.php using $app->configure() right after I register the application itself, so it looks like this:
require_once __DIR__ . '/../vendor/autoload.php';
try {
(new Dotenv\Dotenv(__DIR__ . '/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
//
}
$app = new Laravel\Lumen\Application(
realpath(__DIR__ . '/../')
);
$app->withFacades();
$app->withEloquent();
$app->configure('/Configuration/Lumen/app.php');
$app->configure('/Configuration/Lumen/auth.php');
$app->configure('/Configuration/Tymon/jwt.php');
The files are present in their respective directories, and contain the settings I want Lumen to use instead of the defaults, which are located, respectively, at:
/vendor/laravel/lumen-framework/config/app.php
/vendor/laravel/lumen-framework/config/auth.php
/vendor/tymon/jwt-auth/config/config.php
The problem I run into is that with this configuration, Lumen seems to ignore my custom files, and instead uses the defaults. What am I doing wrong here?
Put your configuration files in config/ and register them in bootstrap/app.php by their filename, without the .php file ending.
// Your custom config in config/jwt-auth.php
$app->configure('jwt-auth');
I don't think any of the above answers is truly answering what the OP wanted to know how to do.
What they appear to want, is to load a composer package and register that packages configuration into the app without having to do any kind of manual configuration.
Sort of like when you import a standard composer package which builds a logger using environment variables and autoconfigures its setup without having to add that configuration to the app. So then things are simpler.
Although I'm assuming the OP knows that this leads to a few problems, in that you're stuck with a composer package configuring your app, with a few options to override those settings locally.
But I assume you're happy with that. So therefore, I'd like to propose this solution
In your composer package, create a providers folder, then add this code into a lumen service provider class, in my case I've called it TestProvider
<?php declare(strict_types=1);
namespace YourLibrary\Providers;
class TestProvider extends \Illuminate\Support\ServiceProvider
{
public function boot()
{
// Because we're using 'require', this needs to be a valid path
$path = __DIR__.'/vendor_config.php';
// Get a configuration object from the service container
$config = app()->make('config');
// An example: Set into the 'app' configuration at a specific subkey
// just to show you can modify the default app configuration
$config->set('app.subkey', require $path);
error_log(json_encode(config('app')));
// An example: Set into the 'vendor.test' configuration at a specific subkey
$config->set('vendor.test', require $path);
error_log(json_encode(config('vendor')));
}
}
As you can see, you can use the dot notation for the first parameter to set() to insert your configuration into the applications config, but be careful with the naming cause in theory I guess you could override any setting like this, it could be that you end up overwriting the entire config('app') and all it's settings which would lead to a partially non-functioning app.
Then inside your bootstrap.php file in your app, you need to register this service provider, like so:
$app->register(YourLibrary\Providers\TestProvider::class);
I've tested it with an app I was working on locally and this works and I'm already using it for a library that has a very static configuration, everything is working pretty great.

Laravel elixir version looking in the build directory incorrectly

I'm using Laravel Elixir (5.2) to do some gulp tasks such as concatenating files and then versioning them.
When I version them, by default, they output into the '/build' directory in my public folder. I don't want this to happen- I want them up one level without the build folder.
Looking at the source code for Elixir, I could see that you could pass a directory as another parameter to the version method and the files would be created there.
However, when you try and run your application, it throws an exception as it is looking for the manifest in the build directory.
Line 295 of laravel\framework\src\Illuminate\Foundation\helpers.php has the following function, which is called in the blade template to get the path for the filename of the versioned file:
function elixir($file)
{
static $manifest = null;
if (is_null($manifest)) {
$manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
}
if (isset($manifest[$file])) {
return '/build/'.$manifest[$file];
}
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
I can fix my issue by removing the hardcoding of the build directory.
2 questions:
1. Is this a bug with Laravel?
2. Is it normal to put public assets into a 'build' directory?
I had the same problem.
You have to have both assets and rev-manifest in the build folder, in order to use elixir helper method.
Even if you create rev-manifest.json with correct paths and store it in build/ so elixir can get to it, it will still create paths to the assets with build/ prefix.
I ended up creating my own helper function to resolve paths from the root folder (basically the same code, without hard coded build/ part)

Laravel: How to include file from Vendor folder in Laravel

I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
The error is {
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):
failed to open stream: No such file or directory
From where do you want to include that file ?
Place a reference to your file in composer.json autoload object:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
echo base_path() . '/vendor';
Should be the route to your vendor folder.
You can se all the documentation in
Helper Functions Laravel
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
It should be something like this, but it will vary depending on the project.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.

Including PHP files with Laravel framework

I am trying to "include" a php script into one of my views (landing.blade.php).
The script is in:
/laravel-master/public/assets/scripts/config.php
When I try to include this code in the view:
<?php include_once('/assets/scripts/config.php'); ?>
I get the error: include_once(/assets/scripts/config.php): failed to open stream: No such file or directory
This is on localhost using MAMP. I'm not sure if there is a different set of rules I need to use with Laravel 4 to include a php file. Thank you for your help!
First, it's not really recommended that you keep your PHP files in the public directory, they should be kept inside the app folder. I'd suggest you create a folder inside app, something like includes and put your files there. Then, you include it, do:
include(app_path().'/includes/config.php');
Although, since it looks like you're trying to load some configuration files, I'd recommend you also check out Laravel's own way of handling configurations. For instance, if you created a myapp.php file inside the app/config folder, Laravel would handle it automatically for you, as long as you'd have some key-value pairs, like this:
<?php
return [
'name' => 'Raphael',
'gorgeous' => true
];
You could then retrieve these values using the Config class:
Config::get('myapp.name'); // Raphael
This is a better solution because you can also take advantage of Laravel's environment configuration.
You can use includes in HTML forget about concatenation
#include('foldername.filename')
#include('filename')
This is another way:
require 'file path';

Categories