Bundle up things in Codeigniter to use in multiple projects - codeigniter

I have a bunch of projects with codeigniter that all use a user controller. The controller does login, registration, user pages, etc. It also has a collection of views and a model. All of this stuff falls under /users/* in the url routes.
It would be great if all of this stuff that I reuse in each project was in one folder so it would be easy to do version management on it alone, and so it was easy to transport from project to project.
Is there a good way to do this with codeignighter?
Note: I use "user" as an example, but there are many things that I would like to bundle up and just drop into projects, such as forums, admin, etc.

If your code is reuseable (also for others) you could roll up a spark for it, see sparks.
If your code is reuseable for you alone you may have to use the HMVC modular setup as mentioned by #Dave and #dianuj or go with git submodule which I personally haven't had too much success with.

Related

Create a Plugin-Based System Like Wordpress With Laravel / PHP

This question has been bugging me for quite some time now, I want to create a cms, a School Mnagement System to be more specific, and what matters to me the most, is making this system plugin based, in a way that administrators can download and install plugins right through their admin panel, just like wordpress.
Now I've read about wordpress Hooks, Actions and Filters, and in all honesty i can say i'm just a little familiar with their functionality, but to apply same functionality to a Laravel App... , It's a bit hard to figure out, at least for me.
I've also searched about this question and found out that almost everyone is suggesting packages, But while packages can work great for other developers wanting to get use of the package's functionality, they can't be easily installed by a user not familiar with any programming language, And that's exactly what i want in my application.
Now what i have in my mind so far, as in Wordpress Actions and Filters, almost everything one would want to use in Wordpress theme files ( to make that piece of code flexible for adding plugin functionality ) should be called as functions, and calling that function by Wordpress do_action() is kind of a must-take path, which of course makes sense.
This way plugin developers can just call add_action() / add_filter() in their files and put their plugins into the flow of the application.
So...
Basically what I'm asking here is:
How to apply such functionality to a laravel app?
Is there an easier way than calling everything by a function like do_action()?
Should i write some kind of a file management module for downloading and updating plugins?
Again I must say that I am not very familiar with wordpress ways to achieve this kind of functionality, So I may be wrong about some of the description I mentioned above, But please, I appreciate any clarification around this subject if you can.
Thanks in advance for any insight.
*P.S. I've tried to be as clear as I possibly can about the problem, And I'm not looking for any specific method for the system I`m developing.
What i want is to know Common / Best methods ( If there is a best method ) for achieving such functionality, preferably in Laravel, but a general algorithm would still be very appreciated.
I know lots of people have the same question and are tackling with the same issue, so if you even only provide a link to a useful article it would be great.*
I have been working as a Wordpress Developer for a very long time and I also have sufficient experience in Laravel... and to be honest, making this kind of functionality is not that difficult at all.
How?
First of all, you must understand that the web app that you will be making will have all the functionalities, but they can only be enabled/disabled from the front-end (Admin Panel/Dashboard). In other words, your School Management System app is actually having all the features/functionalities pre-installed, but you are just allowing the users to enable/disable it. This is much simpler.
So, if for eg. you want to provide someone attendance management functionality, you will have to give the admin of the site, the access to enable/disable it.
Why?
Wordpress has a built-in UI for adding/updating plugins. But in Laravel, everything is code based and therefore this I believe is the simplest way to go (especially for making things in your app pluggable).
Steps
Make a site with UI etc...
Create a config and migration file which will have the names of all the plugins you are going to allow the user to enable/disable.
Make a sync command that will sync all the plugins in your app.
Use it render a page from the front-end and manage their status from the back-end (is_active etc etc...)
Now once, these plugins are active, make sure the user (say moderator, editor) has access to manage them, for which you will obviously have to create roles/permissions and use Laravel Policies for such stuff.
Lastly, only the admin will have the enabling/disabling writes and no one else.
For the other part where you want the people to perform their own functions with filters/actions. I'd say this is completely contradictory to the part where you say Installing Laravel Packages isn't easy. Well, if you would really want a developer a way into this, why not straight away ask them to make a package for your app alone? Hmmmm.... Think about it :)
But, even then, if you would like to create such a functionality, create a simple class which the user can call and user its functions to call their own functions... Something like -> SMSPlugin::do_action(). But then again, making things like updating/deleting the plugin will require you to build your own Plugin Store and ask users to upload to it. Then you'll have to make an API to constantly check for updates for all the installed plugins and blah blah.... It's a really long procedure... And I don't believe that feasible too!
Hope I answered everything. Let me know in the comments if you have any doubts :)
I know it was 2 years ago, but for now you can achieve this with laravel with some packages:
So i think the steps will be:
Create new Laravel App and add some packages for BASIC admin UI and things you will need like:
Authentication (https://laravel.com/docs/5.7/authentication)
Roles and Permissions (https://github.com/spatie/laravel-permission)
Users managment
Modules (https://nwidart.com/laravel-modules/v4/introduction)
At this point you must configure and write a UI for manage every thing from every package mentioned.
So i think this is the way or at least some try to do it in 2019 :D
Happy new year to all!
There's a package for this, it supports version 5, didn't test yet with 6:
https://github.com/oneso/laravel-plugins

There's any issue if I don't place my models into a specific folder inside the app directory?

I am following a tutorial where the tutor places all of his models inside the following path app>models.
I'm not willing to do that, but he is always talking about best practices like the one I'm talking about.
So, there's any problem?
Even though Laravel is a MVC framework (Model-View-Controller), we don't see a folder called Model in L5 and above.
People often tend to add everything in a folder called Model just as to group all models together, but it's optional in L5 and above.
But yeah, it's always good to keep everything under a folder called Model in my opinion.
Laravel just doesn't bottleneck you or your project into using any one method. This is something for you (and your development team, if you're a part of one) to decide on. That is the convention behind Laravel - giving you the developer choice in the matter.

use boilerplatejs with codeigniter

What is the best way to use codeIgniter with BoilerplateJS? should I put the codeIgniter folder in a BoilerplateJS folder or the contrary? Or something else? Need to make an authentifcation page in codeigniter and redirect the application in boilerplateJS.
Thanks.
I tried BoilerplateJS with CI in the following way:
Basically this is including BoilerplateJS in CodeIgniter folder.
I included all the BoilerplateJS code except the index file in to a folder named public which is in the root folder of CodeIgniter. The index file is placed in the views folder and will be loaded by a controller. (See the image)
For this to work some file paths had to be tweaked.
File paths in boilerplatejs index file (boilerplate.html in my case) had to be changed as follows:
./libs/jquery/jquery-min.js >>to>> public/libs/jquery/jquery-min.js
./libs/underscore/underscore-1.3.3.js >>to>> public/libs/underscore/underscore-1.3.3.js
and so on.
In main.js requirejs path configurations should be changed to:
require.config({
//Let's define short alias for commonly used AMD libraries and name-spaces.
paths : {
// requirejs plugins in use
text : 'libs/require/text',
i18n : 'libs/require/i18n',
path : 'libs/require/path',
// namespace that aggregate core classes that are in frequent use
Boiler : './app/core/_boiler_'
}
});
And in your controller you can load boilerplatejs by: $this->load->view('boilerplate.html');
I was thinking of integrating BoilerplateJS and CodeIgniter and probably use codeignighter-rest server for some time.
If all goes well I will share the code within the week.
A sample project is available at: https://github.com/slayerjay/codeigniter-boilerplatejs
EDIT Adding my view on CodeIgniter and BoilerplateJS
Firstly I have not (yet) done any major projects with BoilerplateJS and Codeigniter. But I have done projects using CI and the CI REST Server and know BoilerplateJS in and out.
I do not have much experience with other PHP frameworks (I have meddled with cakePHP and some others) but for me CI helps me to organize my code according to MVC pattern in a clean way, and provides excellent helper libraries and documentation.
As the OP rightly said, authentication is handled best outside BoilerplateJS and this can be done nicely with something like ion-auth for CI. After the user is authenticated and the SPA is loaded, the rest of the calls will be handled by the CodeIgniter REST server.
In this case you won’t be using much of the View aspect of your MVC architecture, but CI’s models and helpers would be of great help.
If you just need a simple REST server you can go with some lightweight solution that just provides REST type routing, but in many cases you will need to interact with a database and do some data processing.
So if you have decided to have a PHP backend for your application, Codeigniter with the REST server is a good choice.

MVC3 How to serve different versions of the site

I need to be able to serve different versions of my whole site per client. Certainly beta vs production but it would be nice to serve one or two prior production versions. Client customization is encouraged and I don't wish to force all clients to follow the (anticipated) pace of development.
Which version to serve is stored in the db.
From what Ive read so far a custom view engine may be the most straightforward way to accomplish this but before heading down this road I would welcome any advice and comment.
Thanks for insight!
Eric
EDIT: It isn't just the views that are different; controllers, master pages, images, CSS...
If the rules regarding which version is displayed to which user, I'm guessing they need to authenticate first. If that's the case you'll have opportunity to set a cookie on the user.
I would probably set a cookie on authentication that determines which version to show the user and then use a URL Rewriter like IIRF to direct requests with that cookie set to the beta version of the site.
Has the added benefit of users being able to opt back to the production site if they have issues with the beta.
I went down the view engine route How do I implement a custom RazorViewEngine to find views in non-standard locations?
Basically it's pretty stright forward, we have the same controllers models, etc. All the code is shared. The rendering of the views though is based on a "brand". So the view engine is clever enough to say if I'm brand A then the view will live in {standard view location}/brand/viewname.
It's hierachical though so if the view is in the branded folder it uses that one, else it falls back to the default location. It's basically an extension of the standard model used by MVC to find the location of the view source.

Zend Framework :: General App Design Question

I just started learning Zend (& OO PHP for that matter), I have spent the last 4-5 weeks learning, tutorials, books etc. I feel good about it but will bog down in the models (thats ok, I'm learning). I am now beginning my first app (for work even); It has at least 5 major sections (including the login + will need ACL), and a couple will have up to 10-12 sub sections like admin: create user, edit user, etc.
I created a single layout, and have made most of the page views with working links, and have a few of the forms complete already.
My major concern now is should I refactor and make modules of the major sections before it gets out of hand, or am I worried about nothing. One thing I think I did wrong is that I have a 'AdminController' that does nothing but bring in the admin 'view' that is nothing more than links to each 'user' action in the 'UserController'. I'm thinking maybe I should have put the user actions in the AdminController. I'm thinking too, that I should make a 'admin' module, 'reports' module, 'auth' module, etc. Or is it normal to end up with 8 controllers and growing? I already have the inclination to make and maintain a developer's sitemap just for my own sanity, not to mention that I want to do the best job possible :)
In principle, I like the idea of a plugin-able module for each set of functionality - News, Users, Galleries, etc. "Plugging in" that module would provide functionality for the back-end admin and the front-end display. It is a self-contained place to put all the functionality - models, action helpers, view helpers, view scripts. etc - that you need for that content area. There might be two controllers per module - News_BackendController and News_FrontendController - dedicated to their specific areas.
But in practice, I find that ZF modules make that hard. I know that smarter guys than me - a low bar, to be sure - can make it all work, but I've never had luck with it.
So I usually end up with two modules - frontend and backend. For news functionality, for example, I'd have a news controller in the backend module for managing the content; another news controller in the frontend module for displaying it.
The sticky point for me in this setup is where to put model functionality that is common to both frontend and admin. One idea is to put it out a separate library and then create module-specific models that extend these for any module-specific functionality. Something like:
MyLibary_Model_News for the common news stuff.
Frontend_Model_News extends MyLibrary_Model_News for any frontend-only news functionality, if any.
Admin_Model_News extends MyLibrary_Model_News for any backend-only news fnctionality, if any.
Just some ideas. As always, YMMV.

Resources