I am currently building an API using Laravel, and in order to learn Laravel as quickly as I possibly can, I am using another person's project to teach me the basics.
I have come across a bit of code that I cannot get my head around and was hoping that someone may be able to tell me what it is actually doing?
Here is the full code:
User::min()->find($this->id)->devices()->get();
Most of the above is obvious to me as I have been building this project for a month and resultantly, am very familiar with models. But the following snippet I cannot understand:
User::min()
Is min() the predefined PHP function, or is it a custom method on the model? Either way, how does it affect the retrieval from the model?
EDIT: Check the User model and see if there's a scopeMin() defined there.
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
I'm currently creating a webpage and I have a problem, which I couldn't find answer for. I'm using Laravel framework and I need to store pages somehow. I would normally use MySQL or SQLite or some other DB engine like that, but some of the pages I write contain PHP code.
Specifically, page /app downloads something from somewhere using file_get_contents, does magic with converting encoding and displays the downloaded content. Page / is just plain Html.
I thought about using eval() but it seems like an extreme overkill and insecurity to me. Also I would like to leverage the MVC somehow in this by having the logic of /app in controller and the text of the page in some DB. So I need to find some type of model which would allow me to this.
I'm using the "function-per-page" approach.
i.e.
Class MainController extends BaseController {
public function getIndex() { // some code }
public function getApp() { // some code }
public function getDocs() { // some code }
}
etc...
Any ideas on how to deal with it?
EDIT:
A little bit of background. I'm not a newbie, it is just that I don't know how to deal with this project. I have experience in writing projects like blogs using Laravel.
The page I'm creating is "portfolio-type"; mostly static, but 2 pages contains scripts as stated above.
Also I already have working version, but I'm rewriting it from scratch. Original version used Slim framework and pages were stored as php files, then included to template based on url.
include "pages/$url_part.inc.php";
As this SO Answer states, there is no way besides eval to execute php code that resides in a database.
My suggestion would be to try and learn a bit about laravel and MVC and to redesign the application afterwards. A resource I can not recommend enough for learning laravel is Laracasts. Laracasts also provides a very well done series called Laravel from Scratch. But chances are good you already know that.
To address your issue once more: I think it is worth thinking about a redesign of the whole app. Since the workflow with laravel is super fast, this is better done sooner than later, because your design will lead to more problems in the future, because it simply does't fit well with laravel. Also, this will open up a lot of nice possibilities, because you can use the full power of laravel.
The Database is what lies behind models, the M in MVC. Only actual Data should be stored there, no Logic (C - Controller) or Presentation (V - View).
If it is just a normal website (like a Blog or sth.), think about not developing it all by yourself. There are great free Content-Management-Systems on the Market (like Wordpress, to name a very popular one), which also offer a lot of possibilities for extension and customization.
Given the little information you gave, this is the only advice I can give so far. I hope this helps a litte, and sorry if this stuff is to basic for your level of knowledge, but it is hard to tell how proficient the OP is in many cases.
My company recently started working with Laravel 4. I'm now in the process of building a basic CMS. I've already found out that there are many ways to reach any goal you can think of and I'm greatly in love with composer and packagist ;) For most anything I would normally build myself I've found an existing package. Just not for this one thing:
What I'm looking for at the moment is a good way to define or generate the basic crud views for any cms controller. For example, image a list of products to be sold and each product has properties ( a few of which are 1-n or 1-1 relationships ). In this case I would either need to build a massive form view to be able to manage all these variables manually, or I would find someway to generate it. If it was just this one time, I'd simply build the form. But because there will be many more similar modules I'd like an easier and DRY method.
I've considered making a artisan generator for this purpose, wich uses the model to figure out all fields and relations. That is great for the first setup, but still requires manual work for special cases or changes afterwards.
So I thought defining the form in a multidimensional array and then using that to generate the nescesary formfields, even with support for Form::macro expressions in the view for very specific needs.
This works fine and I've got it working fairly easily but after adding many whishes ( like nesting of form items in groups etc ) it slowly became more cumbersome to manage.
I've been searching for packages or smart solutions for this problem but so far haven't found anything usable. Hence my posting of this question.
I'd love to hear your thoughts on the subject.
Thanks!
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.
Currently I am working with a commercial project with PHP. I think this question not really apply to PHP for all programming language, just want to discuss how your guys solve it.
I work in MVC framework (CodeIgniter).
all the database transaction code in model class.
Previously, I seperate different search criteria with different function name.
Just an example
function get_student_detail_by_ID($id){}
function get_student_detail_by_name($name){}
as you can see the function actually can merge to one, just add a parameter for it. But something you are rushing with project, you won't look back previously got what similar function just make some changes can meet the goal. In this case, we found that there is a lot function there and hard to maintenance.
Recently, we try to group the entity to one ultimate search
something like this
function get_ResList($is_row_count=FALSE, $record_start=0, $arr_search_criteria='', $paging_limit=20, $orderby='name', $sortdir='ASC')
we try to make this function to fit all the searching criteria. However, our system getting bigger and bigger, the search criteria not more 1-2 tables. It require join with other table with different purpose.
What we had done is using IF ELSE,
if(bla bla bla)
{
$sql_join = JOIN_SOME_TABLE;
$sql_where = CONDITION;
}
at the end, we found that very hard to maintance the function. it is very hard to debug as well.
I would like to ask your opinion, what is the commercial solution they solve this kind of issue, how to define a function and how to revise it. I think this is link project management skill. Hope you willing to share with us.
Thanks.
If you're using codeigniter, just use:
http://www.overzealous.com/dmz/
I don't know what I even used to do without it.
Congratulations, you have invented an ORM :)
There are plenty of commercial ORM solutions but, in my opinion, all they no better than yours. And I'd go for good ol' SQL.
After I did some research on ORM vs Active Record. For my situation I didn't find a lot of help by switching to ORM will help me better.
I found out that ORM is not do very in READ data. But good in Create, Update, and Delete.
My current solution is every model recompile the my own OR_WHERE() / AND_WHERE(), before pass to the $this->db->query(). It is more easy to maintain and customize.