Kohana - comments under article - comments

How to show comments under the article in Kohana v3.2 ? Should I use subrequest to Comments Controller or just call the Comment model in "View" action of Article controller?

There are multiple pieces at play here. How do you get comment data to your views to use in multiple pages...well maybe create a model that does the comment retrieving.
How do you format the comments the same across multiple pages? You can either use a custom helper to format it into some standard html or you can do the view within a view logic to load the comment fragment in into the page.

Id preffer to HMVC calls. With HMVC you can use the same code for internal subrequests (show comment list under the article) and external calls (load/reload comments via AJAX)

Related

How to differentiate between two dynamic url in Laravel

I have two dynamic url with simillar structure. For example, lets say, Product page and category page.
I have set both pages in
Route::get('/{product}', [UsersController:: class, 'productDetail']);
Route::get('/{category}', [UsersController:: class, 'categoryProducts']);
But when I click on url which suppose to go in category page, it redirect to product page only because of same structure. How I can differentiate both URLs for Laravel without altering their url structure?
I don't think this can be done without modifying the URL pattern at least a little bit.
If you do something like /50?type=category then in the show method you can use the query parameter to determine which table to look at. But you'll have to use the same show method and I don't recommend doing it this way.
I hope someone else will be able to shine some more light on the matter.
this is the best practice for your case to make yourapi Resful
Route::get('/product/{product-id}', [UsersController:: class, 'productDetail']);
Route::get('/product/categories, [UsersController:: class, 'categoryProducts']);
learn more about Restful api here https://restfulapi.net/resource-naming/
This should be done by calling index, update diff() function. You can try by using the below:
Route::get('/category/{slug}', 'site\categorycontroller#show')->name('category.show');
Route::get('/product/{slug}', 'site\productcontroller#show')->name('product.show');

Symfony the same variable for all Twig templates

I am writing my own CMS using Symfony 3 right now and i have problem with include the same variables in all controllers.
For example:
I want render logo which i have just upload in my admin panel so i keep URL to it in database and fetch with other data like meta (site descryption, site title etc.) in controller - passing it as array and call in base.html.twig
However base.html.twig extends all other twig templates which i use in controllers so i must fetch it in all of them.
Is there some nice solution for my problem ?
Posting an answer based on your comment.
Use addGlobal() method in your base controller like so:
$this->get('twig')->addGlobal('variable', $variable);
Where $variable is the global variable you need.

Joomla component "attachments" allow html in input

this question might be a bit special. I am using this Joomla 2.5 extensions to give authors the abilty to add Attachments to articles: Joomla Attachments
The extension renders an input field called "description" in a backend form to insert an file description for the provided file. Unfortunately it´s not taking HTML tags which I need. By saving the form it seems a strip_tags() or preg_replace() or something similar cleans the input. I combed through the code of the attachments extension but couldn´t find a place where the input is cleaned or saved.
To hopefully stay in the Question + Answer rule of Stackoverflow:
Is there a class which extensions inherit from the Joomla Core to save form data to a DB-table ( which also could be responsible to clean and validate user input )?
thanks for any idea,
tony
You should see how the field is defined first:
1. Form definition
look into the
administrator/component/yourcomponent/models/forms/somename.xml
there you could find a form definition, if so it will also specify the field type: depending on the type there are several available filters; for example the default textarea will strip html, and you need to set
filter="raw"
in order to enable it. see http://docs.joomla.org/Standard_form_field_and_parameter_types for a list of fields, click and you can find the available format options.
2. model
If the model inherits from JModelAdmin or JModelForm or other JModel* it will automatically handle binding of the forms' data to the database, look for the Save function which should receive the form $data.
3. more
There are at least another dozen possibilities. If the above didn't help, try finding the form: possibly you could find it just by looking at the markup. Once you have the form, check the following fields:
option
task
view
This should help you find the php code that is invoked based on the form:
if view is set, maybe in ./views/someview/view.html.php you could find the saving logic.
if task is set, look for a function with the same name in ./controller.php
if task contains a ".", look for the controller in the ./controllers/ folder.
if option is not the name of your component, your component is sending the data to another component for saving, and most likely set a return-url

Form from another model in a view

So I'm trying to extend the Blog tutorial adding some comments:
Post hasMany Comments
I want to display the add comment form in the same view as the 'post view'. Thing is I don't know the best way to get this approach. I thought about three ways:
Creating a function in Comments Controller to handle the data.
Creating a function in Post Controller to handle the data.
Deal with the data in the same function that deals with the post views.
The main problem with the two first 'solutions' is that the validation errors doesn't show up in the form unless I do some messy hacking of saving the invalidated field in a session variable and then parsing the variable on the beforeFilter callback, like this:
function beforeFilter () {
if ($this->Session->check('comment_error')) {
$this->Post->Comment->validationErrors = $this->Session->read('comment_error');
$this->Session->delete('comment_error');
}
}
What I basically do is adapt the invalidated fields to the actual view and allow it to show properly. This works really well, but it seems so ugly to me. What would be the best approach?
Another related question: should a controller reflect a view? I mean on that example, I thought about only having a Comment Model and dealing with all the data in the controller where's the form to add a comment (even though it's in the Post Controller).
Sounds like you're looking for the Mutlivalidatable behaviour: http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
This allows you to define more than 1 validation ruleset per model. Use your controller to determine which one to apply upon posting something.
P.S. I have only ever used this on a Cake 1.3 project, not sure if it'll work on 2.0.
I see it this way:
Under every post there is an input box "Add comment" with a button to submit.
After submitting some text a form redirects to comments_controller where the comment is saved with this post_id, body, author, date etc.
After the comment is saved and all the logic is done it takes you back to the post.
Under each post there are all related comments displayed (having the same post_id sorted by date or whatever).

Responsabilities of View

After reflection over Fat models and Skinny Controlers (i have adopted), my question goes to view.
It's logic that :
View read Rowset (Zend_Db_Table_Rowset), so object container, or an array of datas ?
View test with Zend_Auth if user connected and show connect or disconnect pictures, or controller test with Zend_Auth and say if user is connected (like a simple data).
View construct url to others controllers/Action, partially or totally without controller datas (plain Example : href="/users/delete/$id"), or view must create url with datas controller (ex : $urlFormat = "/users/delete/%s" and $id = x from controller, and view compose it ($id can be in array with most $id for example in list view, with links to action).
So, view has responsability to format datas, to html, or to xml, to be parsed. But where are borders responsabilities. If you have an article, i can read. Good documentation is very rare.
One more, sorry for my language, i don't write very goodly english. Thanks.
Here are the answers:
No. This should be done by the Controller which will pass the data to the view in a var(s)
No. The controller should do all the tests. The auth test is surely a part of these control
The vars/params should be tested by the controller and the url can be constructed by the view
I think you should read more about the MVC pattern (this is the pattern which has been implemented by the Zend Framework and many others). You can read about this pattern here or wherever you want.
I pretty much agree with Aurelio. However, as for documentation not being too widely available, I have to disagree. Have a look at the list below, which contain both web links and links to text references all about MVC:
Links:
MVC Pattern
Zend Framework + MVC
Zend Framework MVC Folders (ZendCasts)
Books
PHP Objects, Patterns and Practice 3rd Edition (Expert's Voice in Open Source)
Pro PHP: Patterns, Frameworks, Testing & More: Patterns, Frameworks, Testing and More
Aurelio's answered your questions I believe, but here's some extra information. The Controller determines what should happen and put look after retrieving of information from the model.
The view however, isn't concerned with where information comes from, but takes care of the formatting. It's not responsible for connecting to databases, caching records or reading configuration information. I don't mean to be condescending in what I've said and hope that it doesn't come across that way.

Resources