Include Files in Yii - php

I am building an application in Yii that used the SwiftMailer Extension. The SwiftMailer examples show that I should include all of the information in the controller file however would it not be more organized and better to put all of the information for the email in a seperate file and include that file?
If yes, what would be the best way to include that file? I am used to just using the include function but I am assuming with Yii it should be its own class under the components folder?

Ya, you probably want to make a custom class and put your SwiftMailer function in there, as well as any related functions, as appropriate. You can either put it in your components directory or a custom "classes" directory depending on how yo want to import it.
In that class, you can include the SwiftMailer libary (see here for an example of how), in your constructor function or similar.

Related

Alternate to the 'use' keyword in Codeigniter

I am using Codeigniter for a project and I want to incorporate a library into the project, but it is setup with various files using namespaces and the 'use' keyword to include the required files. How do I convert this to work with Codeigniter? Do I just replace every use LibName\Something\Something with a require() statement, or do I do something different?
I'd like top use this library in CI3 unless there's a better way:
https://github.com/webonyx/graphql-php
You will need the use statement in order to use the library. So, don't go hacking graphql-php.
Since the library uses Composer it's very easy to put that autoloader to work with CodeIgniter. In /application/config/config.php set the following "Composer auto-loading" section
$config['composer_autoload'] = TRUE;
Read the comments in config.php if you have changed the location of the "vendor" folder from the usual spot.
After that, you use the standard PHP syntax to use and instantiate classes in the library.
You should probably avoid namespacing your application, meaning anything using CodeIgniter's core classes. That approach has worked for me when combining libraries like graphql-php (e.g. PhpSpreadSheet) and CodeIgniter.

Use Composer library inside Custom Class

I'm using Doctrine DBAL to connect to my database. I created a custom class to act as a kind of Controller (it's pretty much just for retrieving data). I'm using composer's autoload to load this class, so I place it in /src/Digital/Data.php (using PSR-0). Everything works fine, but now I need to use Doctrine in /src/Digital/Data.php, do I have to put require 'vendor/autoload.php';in it? In my index file I also have this (to call the Data class). Which is the proper way to use Doctrine in my custom class?
You need to register the ClassLoader only once. The ClassLoader is registered by the vendor/autoload.php file. If you already included that file in the index file, there is no need to do that in any other file.
The best practice is to include it in the frontcontroller or bootstrap file.

Where in Laravel 4 to declare new location and new namespace

I am trying to create an alternative view and found this answer:
How to load view from alternative directory in Laravel 4
which suggested using this code
View::addLocation(app('path').'/themes/default');
View::addNamespace('theme', app('path').'/themes/default');
But cannot decide where to declare these statements . In which file can I use this code?
start.php,path.php,app.php,global.php or in another file .
If using the app/config/view.php configuration file to add view loading locations (via the paths array) is not enough for your needs, you can probably fit that into a service provider.
Laravel actually uses the View Library's Service Provider to register the view paths locations (based on the app/config/view.php config file as mentioned).
One thing you can do is add your own service provider class and add in your view logic there, in order to add a location / namespaces as you need. (You can even have your service provider read your own configuration files in order to determine locations/namespaces).
If you need help creating a service provider/don't know where to put one, read this on creating a Laravel application library.
If that's all you'll be doing, putting it inside app/start/global.php works just fine. There's really no need for a new service provider for such a simple task.
However, if after some time you realize your global.php file is starting to get too heavy and messy, then you should go for a service provider, as #fideloper mentioned.

How to include a file within the library into a page in codeigniter?

I tried like
$this->obj->load->library('phpmailer');
$this->obj->load->library('phpmailer/class.phpmailer');
But it causes php error.Please suggest a solution.
create a custom loader for the same. You can find how to extend database classes in codeigniter tutorials on the github site of codeigniter, from that tutorial you can get a hint of whats going on and then you can implement how to create custom library loader well if not you can use "core" folder, it is the autoloader for the libraries. check the documentation for codeigniter.
Check these links:
Extending db drivers
Core classes
Go to the phpmailer library and put this instruction
include('phpmailer/class.phpmailer.php');
Your phpmailer and folder phpmailer should reside in the library folder.Your phpmailer folder should contain the file class.phpmailer.php. Now call it
$this->load->library('phpmailer ');
Done!

Include files in Codeigniter

Where is the "best practice" place to put include files (inc.php) when using the Codeigniter framework?
Since you are asking for "best practice", let me give you a long answer:
It's depend what is your included file is about. Since you are using a framework,
CodeIginter have defined various
places for you to place your library,
helper, config, template, etc. It's
better to stick to what the framework offers
or you won't benefit from it.
It's all about the maintainability, the structure of your app, etc.
Don't use a framework unless you are intend to understand them. Use library instead if you are only looking to reuse functionality that people have written.
Mark, put your include files in the view directory as long as they are html, xhtml, php etc. You can make directory's under the view directory to keep files in as well.
yes u can by include Path Helper in your controllers file:
$this->load->helper(array('path'));
then load your file using function file():
$this->load->file('your_folder/your_file.php');
Note that your_file.php file is in root directory in your_folder dir.
I think as a general rule of thumb only include a file where and when you need it.
in terms of config files you'll want to include these as the very first file at the top of the page. This will ensure that any constants and configuratios are available for the rest of the scripts execution!

Categories