custom classes integration laravel - php

I'm currently learning laravel for a small community based website project. I have the necessity to include some custom code/classes in the laravel code. I'll write some custom code that will call some remote api and the JSON response will be converted into some laravel' models and inserted into the database. I would know the right approach to do this in the "laravel 5.5 way" and what components of the framework I should use. Thanks!

It's awesome you are learning Laravel, you will find that you will be able to do so much with it.
You could achieve that by good old php in a controller (in app/Http/Controllers/) and save it from there in the database
A good place to start is laracasts.com as the instructor Jeffery Way takes you from zero to hero in no time.
https://laracasts.com/series/laravel-from-scratch-2017
Have fun.

Related

Building Simple REST API using PHP without framework

I want to build a REST API using PHP, but without any framework. By the following requirments:
The code should be as simple as possible with OOP development principles in mind, easy to read and expand
Data should be kept in MySQL and to be returned as JSON in the given format
DO NOT use ANY Framework or ANY already written code, but to have structure
User input data validation
There should be no security issues
At first, I thought I should build complete MVC project, but I realized that actually I will probably don't need any views and I will use Services instead of controllers. And also models for both entities (Articles and Users).
I'm still not sure what is the perfect way to do it, so I will just tell you what I`m thinking so far...Sorry if Its a duplicate post but I haven't found much information about this and from the little I found, I got more confused.
I thinking of a simple router.php class that will have a method:
map($httpMethod, $route, $callback)
So, for example, I will call ("POST", "/users/register", registerUser(params)) or ("GET", "/users", registerUser(params)), just like I would do in a MVC web app.
I think I will need a model and a service for each of both entities. The service will execute the SQL for each CRUD operation. I think I know how to create the service, as it won't be much different than a controller.
But I wonder how can I create the model part for both entities. What exactly I will need for the models as a code?
First of all, it would be nice if you agree that this is the right way and if not, I would love to hear a lot of criticism because I'm currently confused and really don't know where to start.
I agree with you.
And suggest you to know about Loopback, it's good, like what you described.
I know your question is how to build your own rest api without framework, but it sounds like you'd actually make good use of at least some components (not necessarily a whole framework), do you really need/want to write a router from beginning?
if so sure, if no maybe some microframework? anyway, symfony has some info on how you would create your own framework (just as an example), they use couple of their own classes (i.e. httpcomponent), but just for the explanation of idea/way how things you want are done.
https://symfony.com/doc/current/create_framework/index.html
I found this library in PHP for get started with REST API's
php-platform/restful
This requires prior knowledge on using Composer

Laravel API Generator

I have a project and I'm only responsible for the Back-End development.
In the Back-End I have to create an API that generally have CRUD operations, and the problem is that they are a lot, so to explain exactly here is an example:
Demand => Create a CRUD for Article(Id:number, Title:string, Thumbnail:picture, Content:text, Tags:text)
Explanation => I have to create everything needed so the Front-end can send requests to Create, Read, List, Update or Delete an Article (Return data as JSON)
Problem => I'm searching for a package or a tool that generates that automatically, knowing that a request could contain files or pictures not only text data.
Please Don't Forget
I'm not seeking for a view generator [I'm not working for the Front-End] I'm only responsible for the Back-End
Everything I need do exist in other frameworks, but I want to work with Laravel
I wish you give me tools or packages that you have used not from Google searches because I'm searching for the 4th day
I found this http://labs.infyom.com/laravelgenerator/ but it seems that it have a little bit complicated documentation (Maybe the problem in me)
I'm using Laravel 5
There are several great options for automatic generation of api in laravel you can checkout Dreamfactory, infyom or apiato, I recommend infyom if you have a small project and you need simple api.
It's not totally clear to me what you're looking for. Regarding routes and controllers, Laravel's built-in functionality for Resource Controllers is quite useful in providing general REST scaffolding. If you're interested in all the details of implementing CRUD-like functionality, that's a more involved topic and I'd advise spending some time reading through Laravel's fairly comprehensive documentation.
Try Laravel API/Scaffold/CRUD Generator

integrating CakePHP or CodeIgniter in existing projects

i have a project i took over. it is an app that has been build over many years with PHP and mysql.
It currently has a sort of good folder structure but the code itself is very poor written.
There is php, sql statements and html code in almost every file.
There is javascript code generated using php echo for not reason and so on.
I will like to use for further development either CakePHP or CodeIgniter, even if that means that for the new features some code will be written that already exists (eg.: maybe utility functions) in the old code.
is it possible to integrate one of these frameworks into an existing app?
which one is easier?
do you have any links on how to do it?
thanks.
I have very little experience with CakePHP so my answer is going to be about CodeIgniter. I played with CakePHP for about a day and that was almost two years ago. In my opinion it will probably be easier to integrate with CodeIgniter although someone more experienced with CakePHP might prove me wrong.
Here is the approach I would take. I have never done this, but it seems like a logical way to approach the problem. I suppose this approach would also work with CakePHP.
First, start with a fresh CodeIgniter install using the latest version.
Next, create controllers and actions (controller methods) that mirror the current structure of the application. For example, if you had a page with the URL http://example.com/users/view you would create a Users controller with a view() method.
Next, create view files for each of the current files of the application and load them via the appropriate controller methods. The goal here is to get the application working using CodeIgniter's routing system although at this point you won't be utilizing any models, libraries, or helpers.
Once you have the application sitting on top of CodeIgniter, start refactoring it to fit into the MVC pattern. Pull out application logic (queries, form handling, etc...) from the view files and place them into the controllers. Keep all presentation logic and HTML in the views.
Next, refactor the controllers. This is where it gets tricky because controller code can be placed into models, libraries, or other controller methods. A good starting point would be to take all of the queries and put them into appropriate models. Compare your controllers and see if there is any code duplication. That is a good sign that you should remove it from the controller and place it elsewhere. Unfortunately I can't really tell you where because it differs in each situation.
Continue refactoring your application until you have it in a workable state that you are pleased with...
Hopefully this helps. I certainly missed some critical steps such as setting up and configuring CodeIgniter but if you're serious about doing this I would highly recommend reading through the CodeIgniter User Guide to get a good idea about how it works. You should also get familiar with MVC (model-view-controller) if you aren't already.
There's not really a one size fits all solution here but hopefully I've given you some ideas or at least a starting point to jump off of. If you have any questions or are a little confused drop a comment below and I'll get back to you.
In my opinion, it's easier just to write your controllers in CodeIgniter (I've never used CakePHP) and models, than you just copy paste with some adjustments the views.

How do I make non-framework code to framework code? (PHP)

I just started using CakePHP and it's very different from just normal procedural or basic OOP PHP.
I am still learning PHP and still read "beginning PHP/mysql" books that teaches you basic PHP. Also lots of sites online provide code that isn't for a framework.
Is the only way to make non-framework code to say a framework, say cakePHP, by learning cakePHP thoroughly than rewriting the code yourself to fit the MVC model?
I think it would be beneficial to learn basic OOP PHP thoroughly before learning CakePHP. I admit Cake is a little restricting about naming conventions but you can customize this to your liking. I would try to rewrite a small PHP application in Cake to get familiar with it. Choosing a MVC framework does save a lot of development time if you take the time to learn on well. For Cakephp the book and API are two very valuable tools. I also suggest using Cakephp 1.3.
API
Book
CodeIgniter allows you to write things the way you want but really works best when you use it as a MVC platform. You would write all of the PHP code in the controller if you wanted and the application would still work, you could then decide to split out the code for the database into the model and then later decide to put all of the HTML into the view. That's if you would like to do it like that.
I would recommend doing tutorials and trying to use the MVC model as it has saved me a lot of time and is so much better than flat file coding.
CakePHP can be a little restricting as it requires you to follow particular naming conventions.
Hopefully this gives you a good start on a road to learning the ease and speed of MVC.

How can we added a controller by using WF admin?

My boss give me some assignment to find information "How to add a controller by using wildflower admin (dashboard)? and how can we manipulate it (wf admin)?". I've searched each with Google but it's too difficult to find it. Maybe I'm not so great to do the research job. So, please help me you guys.
Wildflower is a CMS built on the CakePHP framework. A controller is a code construct used in the CakePHP framework. If I understand your question correctly, what your boss is asking for is some way to make the application write code into itself...
However, if I understand your boss' intent correctly, it sounds like he wants something like Drupal's Content Construction Kit (CCK) in Wildflower which, as far as I know, doesn't exist.
CakePHP comes with a console app called "bake". You can quickly create "scaffolding" for a model by running some bake commands. In theory, you could write an extension for Wildflower that could send commands to bake to generate scaffolding for quick add/edit/delete interfaces for those models.... but they wouldn't integrate with the WF admin or WF in general.
With all the effort it would take to build an interface like that, you could just add the new models to the application yourself, or build a proper CCK-like extension for WF.

Categories