Good morning,
I am attempting to setup some test using Silex\WebTestCase to ensure that my application is working properly. In order to do that I need to load up some referential data into the backend storage. I am using ElasticSearch with the https://github.com/doctrine/search library.
I thought that the best way to do this was with Fixtures, namely symfony/data-fixtures. However I am unable to work out how to use them. This is because the fixtures need to implement the FixturesInterface that states the load() method should recieve a compatible Doctrine ObjectManager, however as I am using the Search library I do not think I have such an object.
As this is for test data then I could write something that is home grown but I would like to use things that are appropriate and written for this scenario.
Does anyone have an examples of using fixtures in a Silex application that I could at least look at to point me in the correct direction?
Thanks, Russell
Related
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
Within a Laravel application I have a messenger polling system. With this I'm guessing that every time a user makes a request all dependencies get loaded with every request. Not really a feature I'm very fond of seeing the load it potentially creates.
Is there a way to circumvent most of Laravel requirements to run.
I really only want to insert a message in the database and select a return response and return it as JSON. So I'm not in need of any views, controllers or modules and want to keep those as clean as possible. I actually don't even want to use the PDO and definitely not the query builder.
I'm guessing it saves a lot of server power just to go from the
Route::post(... function({}));
Can anybody confirm?
Alot of Laravel components are "lazy loaded" - so they only actually load when they are needed.
Is there a way to circumvent most of Laravel requirements to run. I really only want to insert a message in the database and select a return response and return it as JSON. So I'm not in need of any views, controllers or modules and want to keep those as clean as possible. I actually don't even want to use the PDO and definitely not the query builder.
To me it seems you want to not use most of the Laravel framework. So if I was you - dont use the Laravel framework. Just use the Request component by itself, or even use the underlying Symphony component.
However, as a side point, it seems like you are trying to optimise something where you dont even know you need to optimise it in the first place. Focus on writing good quality, testable, maitainable code.
The other option is a 'light' framework like Silex
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.
This is what I have:
An entity-relational schema, modelled for Doctrine 2.0 (in PHP);
The generated database, on a MySQL server.
This is what I want:
A very basic CRUD web-interface to the database, that allows me to (you guessed it!) create, read, update and delete records, with extra credit for implementing CRUD operations on entities and relations instead of records.
Now, I'm terrible at writing web applications myself (read: I'm lazy). Are there any options to generate a CRUD web application from a MySQL database, or from a set of Doctrine entities?
I'd be willing to stop using PHP (and thus rewrite the entities for JPA, Ruby ActiveRecord, etc...) but not MySQL.
I see a lot of similar questions: however, most of these questions have answers that give CRUD operations for in PHP code, or recommend using Doctrine.
An answer such as "There is no such tool, stop being lazy" would also be appreciated.
You should have a look at Grocery CRUD.
Really simple, easy to use /deploy and neat UI.
http://www.web-and-development.com/grocery_crud/
I did a complete web CRUD of my DB in a couple of hours (including additional PHP webservices) Amazing :-)
Symfony does this (at least the 1.x series I am used to). I should think version 2.0 also does, under either Doctrine or Propel (and both of those will work with MySQL).
CakePHP (user guide) takes a database model and generates controllers that do basic CRUD operations for all of your tables. It also includes views and a basic stylesheet.
If your hosting setup can handle Python, the web2py framework offers instant CRUD for a database and a very user friendly (and laziness-friendly) online dev environment. I don't think it's designed to be laid overtop of an existing database, but you can import a CSV file with your database contents. http://www.web2py.com
One of the great things about web2py is that creating custom (public) CRUD pages is also dead easy. In a controller file it's as easy as
form = CRUD.create(db.myTable)
return dict(form = form)
Then in a view file you just add
{{=form}}
And that's it! All of the form creation, input validation, etc., is handled for you. I should also add that the data abstraction layer in web2py is very easy to learn and meshes with mySQL easily. One great thing about it is that web2py performs on-the-fly changes to your datastructure or even migrations from one DB back-end to another.
Not every hosting company knows how to support web2py, but it's easy to deploy on the Google App Engine or with a company like Fluxflex.com
I've been looking for a drop-in admin panel like this too, so far I've 3:
AjaxCrud - http://ajaxcrud.com/
Peek from Code Canyon - http://bit.ly/toKKrB
SQLBuddy - http://www.sqlbuddy.com/
Love to hear any other suggestions!
Ruby on Rails' "Scaffolding" should be exactly what you're looking for...
As according to this answer, I've tried Xataface, which gave me as decent a result as CakePHP or Web2Py would have given me. Am now trying Symfony 2.0 (which is a lot harder then I'd have expected) for the extra credit.
I am fond of dbstructure definition of msaccess which lets defining at once and creating the data entry forms, datatables and reports at once easily.
I have been searching for some framework which would generate the data entry forms, data tables and reports easily. I guess the only thing I need to define is complete datatable structure.
Is there any like that or better one than that?
EDIT:
well i am afraid that PHP frameworks have been limited to programmers only. I would like to extend it with some automated functions like autoform in msaccess which would generate data entry form, auto report for data listing. So that my development time would be again some less. I found doctrine nearly matching my specification but not sure as i haven't fully explored doctrine
Cake offers both "hard" (bake) and "soft" scaffolding, which should be very close to what you want. It's still only meant as a quick proof-of-concept tool and to get you up and running faster so you can concentrate on programming the business logic. It's not meant as a hands-off solution nor to be used in production.
What you seem to be looking for is a database frontend like phpMyAdmin or SQL Buddy, not a PHP framework.
Symfony provides an admin generator that builds all the forms on the fly and it will also update itself when you change your db schema. It is based on doctrine which you say you looked at so that would make things a bit easier for you.