Send identifiers between views and controller method with Laravel - php

Working on an application in laravel that has the capability to create article and add photos. With photos you can add a main one to the article or just add photos to a repository.
Once an article is saved to the database it redirects to a view that will allow you to associate a photo with the recently saved article.
I'm wanting to send an some kind of identifier from the create article view to the photo controller method so it will know what to respond with. Then from the photo controller method send that same identifier to the add photo view so when the photo is submitted to the database the controller knows it's a primary article photo.
I'm trying to avoid having to write several different methods that contain a lot of the same code. I would rather the photo controller be able to tell that the photo is a primary article photo and it knows what to do with it.
Any help would be greatly appreciated.

I recommend you to have hidden form field in the view that contains the identifier and photo controller will store it in the session that any other controller can read later.

Related

Is there such a thing as a partial domain model?

I’ve built a PHP ‘personal profile based’ website that has a number of ‘Category’ pages, each of which list a number of Profiles.
Category page: Lists profiles (images/names in a gallery-like layout)
Profile page: Shows in-depth information about one person/profile
Im using a kinda-MVC-esque architecture here. So, when a visitor goes to a Profile page, the appropriate Controller asks the ProfileDataMapper to fetch the relevant profile. The ProfileDataMapper returns a fully formed Profile Domain Object to the Controller which then sends it to a Template View.
The domain objects are all populated from arguments to their constructors, and have validation checks in them so that if any arguments are missing or bad, they will throw an error and not be instantiated.
The problems start when I want to list a number of Profiles on a Category page. Here I am asking the DataMapper for an array of Profiles. But it is returning a list of whole complete Profile Objects when I know that in this case I only need perhaps a Name and URL. It is putting too much strain on the MySQL DB behind the mapper.
The obvious solution would be to simple make the DataMapper pull just the few fields I really need for the list. But then my Domain Model validation tests would fail due to missing data (they are expecting all fields).
So now I am thinking I should create a new Domain model called PartialProfile or ListableProfile. This domain model would just have a name/url and would only validate those two fields. The ProfileDataMapper would return PartialProfiles in its list method instead of Profiles. Maybe the Profile could even extend from PartialProfile.
Is this 'partial model' a good way to go? Or should I refactor my validation code somehow? It seems to me I need different 'levels' of validation. Sometimes I need the object with all data. Sometimes I don't.
Thanks kind people!

ManyToOne relationship where the child entity serves for AJAX file uploading

I have two entities: Image and Post, that are linked by a ManytoOne relationship. The entity Image is used to handle file upload and store upload relative data (like absolute path...). I am using Symfony2 Cookbook tutorial dealing with file uploads.
I am now building a form that allows a user to:
Enter some post specific informations (like title, content..)
upload with jQuery/AJAX many images for the post.
Send the the whole form by button click.
I am still not finding the right approach to implement this solution regarding data persistence in the database.The problem for me is:
In the entity Image, an attribute ($post_id) is used as Foreign Key and will store the post id. The user will upload many images before the entity post is persisted. All the instances of Image created each time will not contain a value for post_id. In my opinion, performance will be affected if I:
update all the rows in image table after persisting an instance of Post.
Create an empty Post instance first, use its id in the Image instances, then update Post instance.
Any suggestions are highly appreciated.
Why don't you use this way of working with your forms?
http://symfony.com/doc/current/cookbook/form/form_collections.html
And in this way you only upload the images with the entire form.
If you need to preview the photos as the user selects them in the file inputs than you can check this answer
How to preview image before uploading in jquery
But it will not work on older browsers like IE8, IE9.

Symfony2 Upload Form & User ID

So I'm trying to create a form in the User Bundle for Symfony where I want to allow users to upload photos and eventually use them as a profile picture.
Using this for example: http://symfony.com/doc/master/cookbook/doctrine/file_uploads.html
I want to figure out how to assign the user ID to the photo while uploading the image as just an "ID".png itself.
Would anyone know? This would take using Doctrine of course.

Post to a user wall and post comments

I am using mobile application (html, jQuery, php) to interact with JomSocial and I have been able to post to groups, discussions and more. The way I do this is look at the controller through the url and go to the actual controller class e.g. /controllers/groups.php and see how it's implemented. So when I see www.mysite.com/groups/1, I know the controller is groups.php
The problem am having is that user's profile is not like this. The url looks like
www.mysite.com/1200-victor-gee-fred/profile
I assumed this would be profile.php but nothing like posting to a wall in that class.
Question: What controller is handling the display of user profile? If you also know how I can post on a profile wall and post reply/comment to it, I will appreciate this.
I know these tables are involved: _users, _wall, _actvities etc. But
the sequence of saving into those tables is what I don't want to miss
and have been successful with other entities except user's profile
wall.
In case somebody might need this in future, I finally found the controller.
components/com_community/controllers/system.php
These functions were used
public function ajaxStreamAdd($message, $attachment) - goes into jom_community_activities table
where $message is the text entered in the "Share" box and $attachment is a JSON-encoded string that contains the target, actor, access-level, app etc
public function ajaxStreamAddComment($actid, $comment) - goes into jom_community_wall table
where $actid is the userid of the user posting the $comment
Best Regards

How to implement multi-upload ques with Symfony2 and uploadify?

I, have successfully implemented uploadify script into my app, files are being uploaded to /web/uploads folder.
I have entity Order witch can have multiple attachments OneToMany-unidirectional relation to File entity, uploadify is a part of Order form.
Now, I would like to transform ulpoaded files into File entity presisted to DB, and set it as related to Order that was created with form.
I suppose I have to add some kind of form ID to Order entity and persist it to DB, so uploadify can send this ID and I'll know witch files are related to witch form instance (Maybe use of CSRF token?)
In general, I have no clear idea of how to implement this feature, my english isn't very good, hope everyone will understand my intentions, I'll be thankful of any help or hints on implementation.
I have done this like that:
Each Order entity on construct gives an unique ID base64_encode(microtime()) witch is persisted into DB, then on files form I get this ID from DB and pass it with uploadify postData, them I have clear link witch file was uploaded to witch Order

Categories