Parsing Laravel Config in PHP - php

Laravel has config files of which are great for storing variables that are accessed from every page on my website. The current app.php config file contains:
return [
'name' => 'This is a name',
'slogan' => 'This is a slogan!',
'primary_color' => '#cc0202',
'secondary_color' => '#990000',
'tertiary_color' => '#FFFFFF'
];
This is just an associative array that of which can be accessed by Blade code using:
{{ config('app.primary_color', '#cc0202') }}
However I wish to use the data from within a .php file that is in the public folder. Files that are in the public folder can not use Blade. What would be the best way to get this data?

If you want to use your Laravel's config in PHP files in public directory just require this file by
$config = include '../config/app.php'; // Go back to main catalog
// and go to config catalog
and use it as normal array.
For example
echo $config['primary_color'];
But in my opinion is very bad practice to put PHP files in public catalog if you are using a framework.

Try this
<?php $variable = config('app.primary_color', '#cc0202') ?>

To access and print:
echo config('app.primary_color');
To set data:
config(['app.primary_color' => '#cc0202']);

Why don't you use a custom service provider instead ?
From laravel documentation:
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.
https://laravel.com/docs/5.3/providers , You can see this helpful tutorial on laracasts to: https://laracasts.com/series/laravel-5-fundamentals/episodes/25

Related

Laravel 8 – use blade asset to display image, but loading from resources subfolder

I’m quite new to Laravel, so patience with me.
I’m in the middle of the process of replicating a framework that I developed in node / react to laravel. Right now, I’m adjusting the main architecture and currently working on a blade master page.
Goal 1:
I want to load images using the blade {{ asset() }} helper in a template page. The thing is that I want to load the images from a subfolder that resides inside the resources folder:
/resources/app_files_layout
I know that I could do this by moving the folder to the public directory, but that’s not what I have in mind. My intention is also to advance on a deeper learning and understanding on the laravel framework and try to maintain an architectural fidelity with my framework on other languages.
Goal 2:
After I manage to load the images from the /resources/app_files_layout through {{ asset() }} helper in blade, I’d like to use an alias for the route in the source code. Example:
Instead of displaying in the output HTML in the browser http://localhost/app_files_layout/image-name.jpg, I’d like it to display: http://localhost/images/image-name.jpg and never show the user that the files are in a app_files_layout folder.
Is what I’m trying to do possible at all in laravel?
Worst case scenario:
If laravel doesn’t have the necessary architecture to do what I want, I could settle for setting up a route that displays the image in the /resources/app_files_layout folder. That is: when I access on the browser http://localhost/images/image-name.jpg should load an image located in /resources/app_files_layout/image-name.jpg.
I tried to follow this stackoverflow suggestion:
https://stackoverflow.com/a/38736973/2510785
However, returned me an error like so:
The requested resource /files-layout-test/backend-layout-header-tb02-03.jpg was not found on this server.
Thanks, in advance!
I managed to do what I wanted for this one. All credit for #donkarnash on this question that I posted for the worst case scenario for creating a route alias for it: Laravel 8 – create route alias for image folder inside public directory
I used the symbolic link approach, almost similar to what #sergey-ligus had mentioned in the comments.
1 – edit laravel \config\filesystems.php file:
Originally, looks like this:
links' => [
public_path('storage') => storage_path('app/public'),
],
After I edited, looked like this:
'links' => [
public_path('storage') => storage_path('app/public'),
public_path('files-layout') => resource_path('app_files_layout'),
],
2 - Then, in the command terminal, I executed the following command:
php artisan storage:link
3 - Finally, I ran the server and tested out. Not only could I access the file in the browser via: http://localhost:8000/files-layout/backend-layout-header-tb02-03.jpg, but also could still use laravel asset blade helper.
Blade file:
{{ asset('/files-layout/backend-layout-header-tb02-03.jpg') }}

Extend blade engine in Laravel to use different views directory

I am working on a Laravel project and I am very new to it. For now, I want to use blade templates to render views but I want it to search for views in different directories like <custom_dir>\views instead of default resources/views.
The <custom_dir> will be dynamic (it can be a variable).
Any ideas? I was thinking of a custom service provider and then extend the default function which renders views in Laravel inside it. But not sure how to implement it.
Edit:
I have user this link to extend the default functionality of include function in blade template engine. But this overrides the include functionality. I want to change the path and then call the default blade functionality
You could probably append the path to the configuration:
1) Statically, by modifying file config/view.php
'paths' => [
realpath(base_path('resources/views')),
//more paths here
],
2) Dynamically at runtime:
$paths = config('view.paths');
$paths[] = $newPathToAdd;
config(["view.paths" => $paths ]);
I suggest you use this in moderation otherwise you will just end up with a mess of directories with no real specified purpose.
You can create custom directories in resources\views directory and use them with something like this:
return view($customDirectory.'.index');
Where index is a template inside custom directory.

Sending variables to Symfony template file

I'm trying to send a variable to a twig file which is not in the typical location, Usually I'm loading views by specifying their path through the Bundle but the file I want to send a variable to is not, hierarchically, on the same level as the other twig templates.
I've a controller which looks like the following:
public function fooAction(Request $request)
{
*//Query*
return $this->render('Bundle:file.html.twig', array('varToSend' => $queryResult));
}
I'm pretty sure the Bundle:file.html.twig is wrong but I don't know how else to specify the relevant path in twig.
Twig you would get from container would not work with arbitrary paths, but you can initialize your own twig:
$twig = new \Twig_Environment(new \Twig_Loader_String());
$rendered = $twig->render(
file_get_contents('/path/to/your/file.html.twig'),
array('varToSend' => $queryResult)
);
If you need this in more than one place, consider making it a Symfony service in order to not initialize Twig Environment every time.
Note that renderer in this case won't have any of Symfony's Twig Extensions, you'll have to add them manually.
If possible, try to avoid this, and put templates into app/Resources/views or src/AppBundle/Resources/views directories.
You have to use a path like that :
return $this->render('Bundle:Something:file.html.twig', array(
'varToSend' => $queryResult
));
And put your file in this folder :
src/Bundle/Resources/views/Something/file.html.twig

How to access language file inside config?

How we can use language file inside config.php php codeigniter?
I searched to find solution just from out side the config we can set item in config to use language file for setting config item value, but how we can set config item using language file inside config file?
for example i want to do as bellow:
$config['item'] = array(
'1' => $ci->lang->line('my_name')
);
You use wrong way. You try make view function in config file. You can make this in template or may be in model.
You should change your application architecture.
you can't access ci object in config file because this is load before CI_Loader class.
but you can update config.php field later on run time, using $this->config->set_item('key', 'value'); function in controller/model/library
sample code in controller __construct(){} method
public function __construct(){
$this->config->set_item('item', array('1' => $this->lang->line('my_name')));
}
Another way use hooks to update item globally, more detail available on CI documentation.

Creating Config File for Custom Library in CodeIgniter

I've looked through all of the CI documentation, and done some Googling of it, but I still can't quite seem to figure out how to create a configuration file for a custom library in codeigniter. If somebody could even just point me in the direction of where in the docs I could find my answer it would be greatly appreciated.
I am creating a library in CI that makes use of several database columns that can vary in name between applications, so I would like the names to be stored in a custom config file. Then I would like to be able to load these values in the construct of the library.
So my two questions are:
1.) How do I name the config file, and how do I name variables within that file so they don't overwrite any other config vars?
2.) How do I get the values from within my library?
When i have questions like this i like to look at other projects that already do this. We utilized Tank_auth in almost all of our ci projects. This is a popular authentication library, which has its own custome config files
It just creates its own config file in application/config directory.
You could prefix your config items with your app name to ensure that they are unique
it then just loads it in the constructor:
$this->ci->load->config('tank_auth', TRUE);
If there is a config/libraryname.php file, it will be automatically loaded, just before library instanciation.
(so, beware of name conflicts with CI's config files)
Side note: this autoloading is disabled if you pass an array as the 2nd argument:
$this->load->library('thelibrary', array('param1' => 'value1'));
in your config /config/your_conf.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = Array(
'your_conf1' => 'your_conf_val1',
'your_conf2' => 'your_conf_val2',
'your_conf3' => 'your_conf_val3'
);
in your controller:
$this->config->load('your_conf');
var_dump((array)$this->config); //show all the configs including those in the your_conf.php

Categories