Website/Files organisation for Ajax calls [closed] - php

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am just getting started with Ajax, and I wonder how to organize my ajax scripts and php files (which process the ajax function).
With Php it was easy, one functions library functions.php with all my classes and methods that I can call whenever I need them.
My first reflex with Ajax was trying to call an already existing method inside a php class in order to process a contact form but I do believe this is impossible and I need a separate file to do this ?
My question is, do I need to create a different file.js for each ajax script and a file.php for each ajax call ? It can get very messy with a few ajax functions compared to php.
Is there a right way to organize this ?

You definitely should use a php framework !
It will help you to organize your code and will bring to you some helpful features like routing which would solve your problem.
You will define some roots, each linked to a specific controller and your code will be well organized.
I can recommend you Symfony2 :
but there are a lot of which are able to do the job :
http://www.hongkiat.com/blog/best-php-frameworks/
If your application is small you can have a look to silex which is a light version of Symfony
To define your urls, you can use a REST api which is a best practice.
I can understand that you find the learning curve of the frameworks difficult but you will not regret it.

Related

How to run PHP as backend and Angular as frontend [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a project and using angular as front end and PHP as back end, installed both in different domains, PHP project is complete and working. I have created API in PHP and in angular I will call that API.
can I do like copy PHP project into angular src folder and run or using different domains how can I call API Or how?
what is the most used way for doing this I have seen tutorials over this but not able to get it correctly?
so can you please tell me any way to work,please give me your suggestion and thoughts
This is quite a big question really.
No-one will be able to give you an answer as to how to do what you're trying to achieve in a single stackoverflow post. It's like asking "how do I build a car?" - there are many steps involved to complete this process. Your best bet is to follow a tutorial.
In my experience, having separate applications for front end and backend is quite common.
You should keep your PHP project separate to your Angular project instead of mixing them.
You should follow a tutorial on how to build a full stack application such as these:
https://morioh.com/p/f2ad5d91a8dd
https://phpenthusiast.com/blog/develop-angular-php-app-getting-the-list-of-items
https://www.techiediaries.com/angular/angular-9-php-mysql-database/
Good luck!

PHP Ajax right structure [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I found a similar questions, but it's not exactly what I am looking for.
I write web site using php. For dynamic content I use jQuery Ajax.
I have 20-50 functions and I want to use Ajax to call these functions and take JSON.
Idea is to pass parametres via ajax POST. Pages works with database and job is done.
The question - how better organize it?
Should I create 50 separate pages like:
mysite.com/ajax/delete_project.php
mysite.com/ajax/delete_user.php
mysite.com/ajax/show_user_info.php
mysite.com/ajax/show_my_messages.php
mysite.com/ajax/show_my_tasks.php
mysite.com/ajax/send_message.php
.......
or create one page? or maybe i am completely wrong with all of that
Ideally you should be using a framework, such as Symfony. Otherwise, I usually keep all the functions for each content type in a single file. So you might have:
mysite.com/api/blog_posts.php which would implement GET, POST, PUT, DEL, etc... for all the blog posts. Meanwhile, mysite.com/api/messages.php would handle that for all messages.
You must start using an MVC framework if not using already. I would say Laravel should be a good choice, it's easy to learn, feature-rich and fun to work with. Start looking into Laravel Routes.
Moving to MVC is the best way to start getting into shape in your case.

PHP seamless application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hello all and thanks for taking the time to view this.
I want to make a PHP application and I am about to start, but I want the front/backend to have as little page loads as possible, but I just don't know what to learn per se. I am torn as to whether I should learn Ajax, AngularJS, EmberJS, NodeJS, BackboneJS, etc?
I really want to know if AJAX is the same as the others I listed or not. I just want to use PHP as the engine to fetch data from an SQL DB and make the application's front/backend seamlessly.
I would like to be pointed in the "right" direction as to which technology to take.
you can use angularJS for faster loading of pages, learning path is first you have knowledge of ajax, then you can learn AngularJs.
here is a reason why you can use angularJs
http://code.tutsplus.com/tutorials/3-reasons-to-choose-angularjs-for-your-next-project--net-28457
and you are going to use PHP as Backend so it eliminate node.js from this list, AngularJs is best as you have describe your requirement, if you know bit of javascript learning cure for angularJs is easy.

When to go for a library file in codeigniter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I always use controller files for my project. Sometimes i get confused when to create libraries. Can you please let me know when do we create libraries files and for what purpose?
I can simply define as following:
Controller: This entry point of your application which is associated with URI.
Helper: Helpers are written in procedural format rather than OOP format. Small scale tweaking is handled by this. As the name suggest Helper file helps the main application controllers in some ways.
Library: Libraries are conventionally reusable code which can be used over different projects.
So if you want to write a library that should be reusable and generic. or it will be a waste of time and effort.
When you need to use any code for several portion or projects, you can create library for better understanding and easy access. Suppose, you want to use any user defined captcha generating function, you can create a library and can use it for several projects.
When you are writing some code that is not specific to current project only and it can be reused on other projects then you should create a library.
For example, I have some generic controllers that I use in every project, I put them in libraries and load via __autoload.

PHP functions from Android - What's the best method? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I was wondering what is the most widely used method for calling PHP functions from within an Android app? The method I was thinking of doing was using a variable in the URL and retrieve it using $_GET from my 'functions.php' and deciding what function to run that way. I just wanted to know if this is the best way of doing it or is there another way people typically accomplish this?
Thanks!
The best way to do this is creating an "API" on your PHP website or app, at this point, you could call an URL using your Android app on the web app, which would execute the method requested.
For example, you could call an URL like this:
www.mywebapp.com/api/call?method=executeCrons&param1=1
Then you'd have in your PHP code something like this:
if ($_REQUEST['method'] == 'executeCrons')
{
executeCrons($_REQUEST['param1']);
}
It's simplified and you may want to add more validations and safeties, but you get the point.
You can also make these functions return a JSON string if you require a result.
There are other ways, such as using eval, but as mentioned already it can be unsafe.
I would take a look at SLIM (http://www.slimframework.com/) for PHP, it makes it super quick to set up a reasonably versatile, secure PHP REST API. I tend to use it to throw together server side pieces because it allows me to change implementation very quickly.

Categories