1

I am using CodeIgniter on a daily basis as a frontend and backend development framework and I'am using dynamic frontend stuff like reacting forms and Ajax very rarely. But I need to say: I love it because it's most user-friendly and that's the key of good frontend development. With forms for example I'm going with to good old way by posting to e new file, validating and pushing it to the database or wherever. I'll like the way of validating it and giving feedback while the user is typing and this is where I came to angular.

First and foremost I like Angular for reacting forms. For the beginning I'll use it with forms only.

How can I combine CodeIgniter's folder structure with angular's folder structure so that I can use first and foremost CI but angular for the form handling.

5
  • frontend in angular and backend in codeignitor thats what you want? Commented Oct 18, 2018 at 10:44
  • Not really. In my head, there is a folder with all the angular stuff and when there is a form inside a CI view (in frontend or backend) I'll put my <angular-form> and the rest is coming from angular. Everything around that form is coming from the CI environment. Commented Oct 18, 2018 at 10:57
  • yes that i was asking, you need to create all the form in angular and rest of the data comes from the CI using an api call. Commented Oct 18, 2018 at 11:01
  • and now back to my question: How do I combine my angular app folder structure with all theses components with my CI? Commented Oct 18, 2018 at 11:12
  • create a project in angular, then in that project create a folder name it whatever you want i prefer api then place whole code of codeignitor in that folder. Now in angular services where you are making http request pass the path http://localhost/project_name/folder_name/action_you_want_to_call Commented Oct 18, 2018 at 11:46

1 Answer 1

2

Angular usually serves the content from AJAX calls, so you should use CodeIgniter as the webservice API framework.

Let's think you're going to implement a simple list:

First, create your Angular project using sample data (by hardcoding values, for example). When you have your product list working.

HTML

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.items = [
    "One",
    "Two",
    "Three",
    "Four"
  ];
  
});

angular.bootstrap(document, ['myApp']);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-controller="MainCtrl">
<ul>
  <li ng-repeat="item in items">
    <a href="">{{item}}</a>
  </li>
</ul>  
</div>

For now, elements are hardcoded. But we need this elements to be dynamic, with CodeIgniter served data.

For that, create a folder named 'api' at the 'www' folder of your server. Then upload all the CodeIgniter source files. If you have done it correctly, you should see the 'Welcome' controller when you access 'http://yourdomain.com/api'.

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.

A brief example:

<?php

header("Content-type: application/json");

class List extends CI_Controller
{
    function __construct()
    {
        // Here you can load stuff like models or libraries if you need
        $this->load->model("list_model"); // For example
    }

    /**
     * This method receives a parameter so it can 
     * filter what list wants the client to get
     */
    public function list1($list_number)
    {
        $list = $this->list_model->getList($list_number);

        // If list not exists
        if ( empty($list) ) {
            $this->output->set_status_header(404);
            echo json_encode(
                "success" => false,
            );
            return;
        } else { // If has returned a list
            // CodeIgniter returns an HTTP 200 OK by default
            echo json_encode(
                "success" => true,
                "list" => $list,
            );
            return;
        }
    }

}

Now we can take the info by AJAX. The same code above but changed to get the remote data:

var app = angular.module('myApp', []);

app.controller('MainCtrl', ['$scope', '$http', function($scope, $http) {
  // Replace link bellow by the API url
  // For this example it would be:
  // http://yourdomain.com/api/list/list1/1
  $http.get("https://codepen.io/anon/pen/VExQdK.js").
  success(function(res) {
    console.log(res);
    if ( res.success == true ) {
      $scope.items = res.items;
    } else {
      $scope.items = [];
    }
  });  
}]);

angular.bootstrap(document, ['myApp']);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-controller="MainCtrl">
<ul>
  <li ng-repeat="item in items">
    <a href="">{{item.name}}</a>
  </li>
</ul>  
</div>

This way you can get a completely functional CodeIgniter API working with Angular. I like to organize methods in different controllers so code is structured to be "readable".

To modify or delete data on the server, you can use $http.post and send parameters to tell CodeIgniter which kind of operation has to do. Remember to use session data to protect the ajax calls of modifying/deleting information (for example if a user tries to update other user's info).

This is not a definitive way, but it's mine. I hope it helped you.

Sign up to request clarification or add additional context in comments.

7 Comments

Wow this is a very detailed answer and it helps a lot! Thank you!
One question stays open (and that's why I had the question generally, I think). When I create a new angular project with ng new app-name I got a folder structure with all these files. Your solution is creating the modules and components directly in the code where I need it (what I like). I'm very new to angular and I don't know this method and you're code looks different to my files within my app folder of my app-name project. Can you give my a hint to where I can research in more detail to get my current angular knowledge (based on a ng new environment) working with you're code?
What I try to explain is that you have to keep your folder structure the same. The only thing that is changing is the information source and how your app interact with the database. All read and write connections are through AJAX, running with CodeIgniter's API. Do this answer your question?
Sorry, is doen't. Maybe my question is too basic. You're code snippets run without the complete ng new app-name environment. There is no app.module.ts and no app.components.ts. It's all implemented in the code. My environment looks like this. For example how can I import { ReactiveFormsModule } from '@angular/forms';?
Ah! I think you're working with Angular 4, not with AngularJS. Angular 4 is a more recent version of Angular (it's better though). I can provide you code for it, I have some projects working in Angular 4 too with a CodeIgniter implementation. (the CodeIgniter part is identical). Wait, today I will update my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.