Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining Angularjs and CodeIgniter

I am working on an existing site written in CodeIgniter and we are looking at using AngularJS for some pages that require a lot of frontend functionality but we don't want to replace all all CodeIgniter views (at once (yet)).

So i click a link that's controlled by angular's router and it is handled by javascript but next link could be a "normal" request that should handled by the CodeIgniter framework.

Is there some elegant way to combine these two methods? I don't really mind some extra client side overhead because the site is not running in production yet.

like image 755
Han Dijk Avatar asked Feb 04 '13 08:02

Han Dijk


People also ask

Can I use angular with CodeIgniter?

For this, I recommend to use this CodeIgniter plugin that allows you to easily create a Webservice API Rest. The main objective is to serve a json object when the Angular asks for data. Then Angular will do the rest. This way you can get a completely functional CodeIgniter API working with Angular.

Can I use AngularJS with PHP?

If you're wondering whether you can use PHP alongside Angular, the answer is yes. But Angular will still need a separate client-server architecture. In general, PHP runs on the server-side while Angular runs on the client-side.


2 Answers

It sounds like you're looking to gradually make less use of CodeIgniter's (CI) routing as your angular application grows. This is not difficult but requires a lot of detail. Which method will work depends on your project structure. Note: I removed index.php from Code Igniter URLs, so the paths below may be different than default.

1) CodeIgniter installed in root

If CI is installed on the root of your server, you can create a folder within CI (for instance I have an "ng" folder). Your project will look like:

    /controllers
    /models
    /ng
    (etc)
    /index.php (code igniter index file)

place an .htaccess file within /ng with the following:

    Order allow, deny
    Allow from all

This allows the files within /ng to be directly accessed, rather than sending those requests back through CI's routing system. For example you can load this directly now: example.com/ng/partials/angular-view.html

The main web page will still be created by CodeIgniter, but it can now include Angular assets, such as partial views, etc. Eventually you can replace most of what CodeIgniter is doing by just returning a simple page, and having Angular load partial views from /ng like it's designed for.

This method is nice because CodeIgniter can control whether that initial page is loaded at all (via some user authentication code in your CI controller). If user isn't logged in, they are redirected and never see the Angular app.

2) CodeIgniter in Directory

If CI is installed in a directory, such as example.com/myapp/(code igniter) you can simply create a directory next to it, example.com/myappNg/

    /myapp/
    /myapp/controllers/
    /myapp/models/
    /myapp/(etc)
    /myapp/index.php (code igniter index file)
    /myappNg/
    /myappNg/partials/
    /myappNg/js/
    /myappNg/(etc)

Now in your Angular application, you can request resources from CI by making paths relative to the domain root, rather than relative to the Angular app. For instance, in Angular, you will no longer request a partial view from the Angular folder partials/angular-view.html, rather you'll want to request views from CI /myapp/someResource. Note the leading /. "someResource" can return an html document, or JSON or whatever you're doing with Code Igniter in the first place.

Eventually you can replace the number of paths which reference /myapp/. Once you no longer use CI for anything, you can simply place your Angular index.html in /myapp/ and it will continue to reference your paths at /myappNg/.

TL;DR Make your Angular app fully available and decouple it from CodeIgniter. Gradually move toward using Angular partial views and other JSON sources instead of linking to CodeIgniter pages. Eventually replace your CodeIgniter endpoint with an HTML file which bootstraps Angular.

like image 57
Aaron Martins Avatar answered Sep 21 '22 23:09

Aaron Martins


Your best bet is to keep your backend code separate from the angular code and use the codeInginter code as an API

/Codeigniter Code /Angular Code

Because CodeIgniter comes with its share of security feature this should be your best bet

like image 21
Sydney Collins Avatar answered Sep 20 '22 23:09

Sydney Collins