Where should ajax backend files be placed in codeigniter - php

I am working on a CI project. I have a bunch of view files from which ajax calls are made to PHP backend files. I want to understand within the structure of codeigniter where I should place these backend files? I dont think I can put them as helpers. They are not really model files, nor are they controllers. Where is a good location to put such files which accept ajax requests and update backend db?

We use a separate set of controllers serving a RESTful API within our CodeIgniter project, returning results to jQuery AJAX requests.
eg:
/controllers
Normal application page controllers (HTML/PHP views)
/controllers/api
API controllers (JSON/XML views)
Both can share the same models obviously. Works well for us!

Related

Laravel 5 and Angular 6 routing issue

I'm having issues when trying to consume a laravel route defined on the api.php routes. When running both (angular and laravel) independently they work fine, but I compiled the angular project and placed on laravel's public folder, changing the index.php to index.html and it loads perfectly (dashboard show's as it should etc). But the service returns an html... Not sure if this is the correct way to do this but...
Thanks in advanceenter image description here
this is not a best way to create a project with angular with laravel.
angular provide separate routing structure also laravel provide self routing.
you need to communicate both of them so you need to create a new url and send this url in you get, post.... request and receive in your laravel routing.
you need to get more information about laravel routing go here.
https://laravel.com/docs/5.7/routing

Symfony2 ignore certain route

I have the following situation:
I'm running a Symfony 2 project on a server with the route www.homepage.de.
Every request to a random route on www.homepage.de will be routed to my Landing Page.
Now I want to integrate another project, an API build with Slim PHP and Swagger UI, into this Symfony 2 project. I cloned the project into the /web directory of the Symfony2 project to gurantee access to it.
But now every request I want to do to www.homepage.de/api fails because the Symfony 2 project wants to handle the request and can't find the route. Is there a possibility to tell Symfony2 to ignore every request that is send to the www.homepage.de/api route?
First of all don't add anything to web folder of your application. Just create two applications in different folders. If you want the API to be accessible through the same domain but just with /api you cen use either mod_alias or mod_rewrite to achieve this.
In your current solution it is not Symfony that is intercepting your requests. Take a look at your .htaccess. This is where the magic happens. You can always modify rewrite rules to intercept all /api requests and redirect them to your application but I would still suggest to keep those projects apart.

called controller page of shopping_admin into ecommerce codeigniter application

i have created one codeigniter application named as ecommerce. In this application i have created another codeigniter application called as shopping_admin into the application folder of ecommerce. so how can i called controller page of shopping_admin? how to configure shopping_admin?
What do you want?
Why you did not make single application and use the ACL (user Access Control List) between admin and user.
But if you still insist make 2 application on one directory, maybe you can use CURL.
Example here
sory for my bad english

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.
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.
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
I've never used Angular - nevertheless this may help.
So i click a link that's controlled by angular's router and it is
handled by javascript
Does this JavaScript make an Ajax request to one of your CI's controllers? If so, CI now has the is_ajax_request() method, which allows you to check if a request (POST or GET) is coming via ajax. You can proceed differently based on a request coming from Ajax vs a normal request.
User guide (bottom of the page): http://ellislab.com/codeigniter/user-guide/libraries/input.html
Hope it helps!
I inherited a CI app and I'm using Angular with CI mainly for routing requests. In my case I am not using Angular templates, so I use a ' ' empty but with a space parameter for the template option in my $routeProvider config. This allows me to do the usual CI ajax requests without too much change to the original server-side code.
angular.module('my_app', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', { template: " ", controller: my_routes.mainpage}).
when('/design/:designId/:action', {template: " ", controller: my_routes.show_design}).
when('/vote_design/:designId', {template: " ", controller: my_routes.vote_design}).
otherwise({redirectTo: '/'});
}]);
To addition to the answer given by Aaron Martin, one can also use it as a client - server approach.
Lets say we make 2 folders in root of our project :
Client
Server
Client folder will contain all the code of AngularJS and the client side libraries including the Bower and Npm libraries.
The routing of the client side will also be handled by AngularJS router.
There will be factories or services which will act as providers for angularjs on client side.
Those file will contain the code of sending request and receiving response from server side.
Server Folder will have the code of Laravel or CodeIgniter or Any other PHP framework.
You will create all the APIs of the requests and develop the functionality accordingly.
Hence the PHP section (Server Directory) at the whole will be storing all the Media Files and Database Files. Moreover it will also have any receiving links for RSS feeds and so on.
The Client shall just receive all the response in JSON or XML format when it requests on any API on its server..
This according to me is one of the finest practice for developing Webapps.
I picked up a CI site from another programmer I work with that is on leave for a few months. Our site is build mostly with a lot of angular due to the the nature of its purpose. Our solution was a little different.
All that varies from the standard CI framework is a couple folders: js\angular\controllers andjs\angular\modules in CI's application folder, to hold all of the angular model and controller files. Then load the angular docs into the application base folder.

Seperate Slim from HTML/CSS/JS

So I am confused on how to properly implement the Slim Framework into my use case.
I have a web app html/css/javascript that can not have PHP in it.
All I want to do is use Slim to do some simple GET requests via ajax.
How do I start the App to handle requests if it is never touched int he index.html file?
I'm guessing this has something to do with the .htacess file but not finding anything useful in my initial search.
Got some help on this from the Slim Discussion board.
Here is the answer and example.
Yes, the .htaccess file ensures that any request is handled by a single file (usually index.php but you could name it anything you want).
Your Ajax calls would presumably go to different URLs. Those requests would all be handled by the Slim app.
Example Here

Categories