I have a custom PHP MVC project with the following structure:
app
controller
model
view
core
public
I want to integrate some libraries like mPDF and PHPMailer. My question is where to place the files?
Should I add the files to core directory or use composer with vendor directory to integrate these files?
In case of composer method how to call one of the libraries?
In my project, I can just create an instance of a class like that $user = new \app\controller\User()
In this case to create instance from a library I have to include the autoload file first?
use composer vendor repository, and namespaces of libs in your controllers, include them by use ...;, and yes, you need to use autoload
Related
I have PHP example on how to use Yelp Fusion API. It uses OAuth.php file with several classes. In main example it is imported with
require_once('lib/OAuth.php');
Can I do the same in Laravel?
Or I'd better provide namespace for OAuth.php file and put it somewhere on the tree? Where to put it?
I suggest your to make a new directory inside app and call it like "Classes" and store your OAuth.php as "/app/Classes/OAuth.php".
Don't forget to put a namespace App\Classes; to top of this file.
Due to having several classes inside your file I suggest to rewrite this a bit and separate each class to file
You can use the Yelp Fusion API with any OAuth implementation. To simplify the process and maintain a clean project, you can use an OAuth package.
You can find a list of OAuth packages for composer here.
Using packages instead of custom PHP files would help to keep project clean.
If you do need to add any custom PHP files, it's recommended to create a directory within the /app directory for organization. A good directory name could be "Libraries" or "Helpers," depending on the type of content being stored.
Note that directories and files within the /app directory are automatically loaded by Composer.
I have a ZF2 model under a module and I want to call functions in this model from a plain PHP file. This file is located outside of the project folder.
Is there any way?
I'm trying to learn zend framework. I managed to install it on my localhost. However i'm having trouble understanding the folder structure? There are 5 main folders after installing the skeleton application - config, data, module, public and vendor.
I've seen some proposed folder structures online, but how to I go about it? Do I just create folders like views, controllers, models etc?
Thanks!
Vendor is where composer installs dependencies and libraries, config is where configuration lives, data is for cache etc, public is where your index.php and css/js/img assets are, you are really interested in module directory that contains application modules. For the start you only really need one module - Application, inside this directory you should have config dir that has module specific config, Module.php - module bootstrap file, view with templates structured per controller and src folder with your code. Inside your src file there is your Application module namespace directory that is placed in Application directory to mimic PSR-4 autoloader namespaces it can contain your application code in this example directory structure: Controller, Form, Model. Model can contain Service, Repository and Entity folders
If you just got started with ZF2 I suggest reading some documentation. Basic things like this can all be found in the documentation. For example here you find more about the folder structure.
I would also suggest taking a look at the ZF2 Skeleton application documentation/tutorial since this will help you understand the basics of a ZF2 application. Here an example on how to structure a new module. Building the album application yourself is a really nice way to get started.
In my project I have some features that can be extended by end user on production server by just uploading a class file into a specific directory (like Wordpress plugins directory) so this classes can be added and removed dynamically at any time.
Currently I'm doing this using spl_autoload_register function.
Can I make this functionality available using Composer, in order to make the project more standard?
The answer is probably yes, if you stick to PSR-0 or (preferred) PSR-4 standard with your class and file names.
I'm a developer who is just migrating from CodeIgniter to Laravel. In CI I had library folder where I could put non database functions, for example email verification etc. In Laravel I couldn't find something like that ? Where can I put such libraries in Laravel 4?
If it is single PHP file containing different functions then you can create a Model class in your Model folder. But this Class will not extend Eloquent. It is a simple class. For example I have a class as General.php containing static methods of general functionality. As Model is autoloaded so I don't need to worry about including file. It is automatically included.
But if it is a directory containing different PHP files or PHP classes then you can create a directory in your app directory and add the path of that directory in providers array in app.php in config directory. You can also create an aliases of your library by putting entry in aliases under providers in app.php.
Create a library (just a folder), namespace it, and set it in config as a provider.
Then you can call it like
new \Foo\Class()