Is the proper way to make a few variables available to all my controllers to add a MY_Controller.php file in my /application/libraries/ folder (shown in the docs here)?
I'm working in Kohana 2.3.4 and wondering if there are any better ways to do it, or is this the only recommended method?
Being new to OOP, can you link me to any examples?
I've heard the right answer is to add the vars to your $config[], trying to get more details.
The proper way is to make a custom config file (application/config/foobar.php), and access the data with Kohana::config('foobar.key').
The code igniter way is completely wrong and inappropriate.
See http://docs.kohanaphp.com/core/kohana#methods_config
How does this feel then:
[bootstrap.php]
Kohana::$config->attach(new Kohana_Config_File('global'));
And then, create a new file under application/config called global.php
In it, put (for example):
return (array ('MyFirstVar' => 'Is One',
'MySecondVar' => 'Is Two'));
Anywhere in your code, access these variables with
Kohana::config ('global.MyFirstVar');
As you can see, 'global.' is used to access these variables; the reason for that is that you attached the global.php config file at the beginning.
Was this what you meant? :-)
Related
I have several projects on my host. They have many similarities and I want them to use common node_modules. My folder structure looks something like this:
_CONFIG.php
node_modules/
project1/
index.php
project2/
index.php
project3/
index.php
In all my index.php files, I require_once '../_CONFIG.php' where I've set:
$node_modules = '../node_modules/';
Then in each project, when I need to use a Node module, I have:
<script src="<?= $node_modules; ?>jquery/dist/jquery.min.js"></script>
I do this because I might need to change the path of node_modules. If everything is hardcoded, I'd have a rough time. Let's say I install upgraded node_modules and I want to have a backup. I want to be able to quickly switch between the old and new modules, in case some of the new ones are incompatible with one of my projects.
I have other common things between the projects that I want to be easily configurable via the _CONFIG.php file.
Question
I know that polluting the global scope is not a great thing and that's exactly what I'm doing. Is there a better way to do this? In the same time, I don't want to do a ton of stuff just to output a string in a script tag's src attribute.
What about defining constants? It's quite useful for defining global options, and you do not pollute the global scope at all.
define('NODE_MODULES_DIR', '../node_modules/');
and then later use it there:
<script src="<?= NODE_MODULES_DIR ?>jquery/dist/jquery.min.js"></script>
I'm not aware of a "better" solution.
Keeping configuration outside of your code is the way to go. You put all your connection strings, pwds etc in a config file as suggested by The Twelve-Factor App.
Instead of using global variables, you could add one level of indirection by using a wrapper function that returns values based on a given key-string.
phpdotenv provides these capabilities nicely.
I come from the procedural PHP and am learning OOP with Laravel. What I learned so far is very interesting and will ease my developer's life (it's not my job btw).
So, for all my websites, I am using a slug property for all articles, categories, and so on.
I started to use the "str_slug" provided by Laravel which seems to do the job at 99%. The issue I get is when I have such title (in french): "J'ai mangé une pomme", the slug string I get is: "jai-mange-une-pomme" which, in french, is not correct. I would like "j-ai-mange-une-pomme".
It's not really an issue. I can do:
$slug = str_replace('\'','_',$input['name']);
$slug = str_slug($slug, '-');
It suits me well but I wonder how to use anytime I want to use it. I don't want to write it again and again and again.
In procedural, it's easy, I would write a function, such as thePerfectSlug(){} in a helpers.php file (still an example) and will use an include at the top of my index.php. That would do the job.
But in OOP and especially in Laravel (5.1), how can I do that?
Thanks
You still can achieve it with normal function. Laravel uses his own function which are stored in helpers.php file. You can make your own helpers.php file and add it to your main composer.json file at autoload.files.
If you would like to do it in OOP way, create a trait like App\Traits\Sluggify with your method and use it in any class that needs it.
Ok,
So quick question that is driving me crazy with FuelPHP
For css, js, and img assets I can do this in a view
<?php echo Asset::css('main.css'); ?>
But if I try to add a folder - for example - "media" to the public/assets directory I can not do this:
<?php echo Asset::media('myvideo.mp4'); ?>
Does anyone know if there is a way to configure that kind of functionality? Has anyone been able to modify the asset class to do that?
Thank you very much for your time and help!
I don't know if there is such fashion that you could create your own static method name (the one you like is media as I haven't tried it yet). But you declare that path instead. Consider this example:
(file is in public/assets/media/file.mp4)
Controller
Asset::add_path('assets/media/', 'media');
View
Video 1
You can easily create such methods by extending the core class.
This can not be solved generically (for example with a magic __call()), because the class would not know which HTML to generate for "media" (or whatever you want to add).
I have just started playing with laravel haveing come from codeigniter and I am trying to work out the best way to define a series of constants.
CI uses a constants folder under app/config and I was largely happy with this approach for most things but wanted advice as to the best way to do this in Laravel.
My constants fall into 3 categories and I would like advice if possible on how best to store and retrieve each (baring in mind I'm completely new to Laravel.
Type 1:
Constants which need to be loaded everytime a controller is called:
e.g. I would like to start by defining a constant to tell me if the user is requesting content via ajax, this is what I used to do in CI constants file:
define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
Type 2:
Constants which can be changed by the user:
Are these best stored in a database or would writing to a config file be a better option? Again looking for advice on how to store and retrieve
Type 3:
Constants which are only needed in specific controllers:
Ideally I would like to be able to group constants into arrays or separate files and pull them out in groups as required.
e.g. I could just retrieve my upload settings for my upload controller.
Thanks for any help/advice in advance - examples would be greatly appreciated
Type 1
You can add any constant in Bundle (or application) start.php file.
Type 2 and 3
I would suggest using Config for both these requirement.
My solution is create a file inside app/config folder called "constants.php"
app/config/constants.php
The code inside is in array way.
"some value",
'c2'=>"some value"
);
?>
then inside each controller you can retrieve using
Config::get('constants.c1');
I think is an easy way to do it, it's more scalable.
<?php
//file : app/config/constants.php
return [
'IS_AJAX' => isset($_SERVER['HTTP_X_REQUESTED_WITH']
];
in anywhere:
echo Config::get('constants.IS_AJAX');
I've used codeigniter in the past but on my current project I'm making the switch to Kohana. What is the best practice on constants?
In codeigniter there is the actual constants.php, but going through Kohana's source I'm not seeing something similar.
Never used kohana, but after a quick googling I find that you can use the config API to create your own config that will house the constants you need.
This thread suggests that if you are storing database sensitive items, to place them in the database.php config, etc.. making them relative to the type of data they are storing.
I'm familiar with Kohana, but not CI so much, so I'm guessing a bit to what you mean by 'constants.' I believe the closest thing to this is indeed Kohana's config API. So, if you wanted to make templates aware of some site-wide constant like your site name, that's a great thing to use the config API for.
To accomplish this, you'll need to create a config file under your /config folder, probably in the /application directory. What you call it isn't very important, but since it contains site information, let's call it site.php.
To quickly get going, here is what you'll want to have in that file:
<?php defined('SYSPATH') or die('No direct script access.');
return array(
// Your site name!
'name' => 'Oh me, Oh my',
);
Now, you can bring this in to a template by doing something like:
A better way to do this (using dumb templating) would be to assign this as a template variable in your Controller. So, assuming you have some default controller set up, the code would be:
public function action_index() {
$this->template->site_name = Kohana::config('site.name');
}
And then your template would have something like this:
<title><?php echo $site_name; ?></title>
Kohana's config API is interesting because it is hierarchical, meaning you can override and merge new configuration values on top of existing config structures. When you call Kohana::config('site.name'), the engine looks through all the config files named site.php, runs all of those config files and merges the results in to an array. The application-level config files will overwrite modules, which will overwrite system, etc... Then, based on that result array, Kohana will attempt to find the 'name' key and return it.
Assuming you want global constants...the following worked well for me.
application/config/constants.php:
define('MY_COOL_CONSTANT', 'foo');
return array();
index.php:
Kohana::$config->load('constants');
MY_COOL_CONSTANT should then be available globally.