Laravel 5.4 displaying different post on different url - laravel-5.4

This is a weird one to me...I have a show method in my PostController:
public function show(Post $post)
{
return view('post.show', compact('post'));
}
Now when I do a dd($post) without returning the show view, I get the correct post. But when the view is rendered, it is displaying content for a different post entirely...eventhough the url is still pointing to the intended post. So if I wanted to show post-1 for instance, the url is loaded correctly (mysite.com/posts/post-1), but the content displayed on the page from {!! $page->body !!} is from a different post (eg from post-2). Never experienced this before...any thoughts will be helpful.

Related

How can I differentiate between two url requests from different HTML pages but with the same namespace in views.py?

I am creating a simple eBay like e-commerce website to get introduced with django. For removing an item from the watchlist, I placed two same links in two different HTML files, that is, I can either remove the item from the watchlist.html page or either from the item's page which was saved as listing.html. The url for both the pages look like this:
Remove from watchlist
Now, in my views.py, I want to render different pages on the basis of the request. For example, if someone clicked Remove from watchlist from listing.html then the link should redirect again to listing.html and same goes for the watchlist.html.
I tried using request.resolver_match.view_name but this gave me 'removeFromWatchlist' as the url namespace for both of these request is same.
Is there any way I can render two different HTML pages based on the origin of the url request?
Also, this is my second question here so apologies for incorrect or bad formatting.
You could check the HTTP_REFERER in the request.META attribute of the view to get the url that referred the request as so:
from django.shortcuts import redirect
def myview(request):
...
return redirect(request.META.get("HTTP_REFERER"))#Or however you prefer redirecting
https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.META

Direct link to YouTube comments

Is there a way to link directly to the comments section of a YouTube page?
I know that this can be done using anchors and div ids, but this has been unsuccessful when I applied it to a YouTube URL, because YouTube strips the forward slash on page load.
For example, https://www.youtube.com/watch?v=eRsGyueVLvQ/#comments becomes ?v=eRsGyueVLvQ#comments
Is this possible, or should this be chalked up to a feature request?
You can make a certain comment appear at the top of the comment section by clicking on how long ago it was posted (e.g. 2 years ago).
This will take you to the same YouTube video, but with a URL which looks something like this: https://www.youtube.com/watch?v=VIDEO_ID&lc=COMMENT_ID (just like in Mr.Rebot's answer).
You can also do this for replies as well.
If you will use the CommentThreads:list:
Returns a list of comment threads that match the API request parameters.
Code Snippets:
// Sample PHP code for commentThreads.list
function commentThreadsListByVideoId($service, $part, $params) {
$params = array_filter($params);
$response = $service->commentThreads->listCommentThreads(
$part,
$params
);
print_r($response);
}
commentThreadsListByVideoId($service,
'snippet,replies',
array('videoId' => 'kmXXXLBL3Nk'));
Then you can create a link with with the URL:
https://www.youtube.com/watch?v=VIDEO_ID&lc=COMMENT_ID
This link is not generated in the API so you should create a function for this.

Yii2: data-method='post' is sending GET request

I am making changes in existing web page in yii2.
I had this section of code:
Html::a('Confirm!',[
'default/apply',
'confirm' => 1,
'id' => $data->id
],['class' => 'btn-primary','data-method' => 'post'])
I have moved this to a different container on the same page.
(I had to adjust slightly, changing $data->id into $projectInfo->id as earlier it was inside anonymous function within a widget and now inside a foreach loop. But this should not be relevant I suppose.)
Both before and after the change the same line is present in html (but in different part of the page):
<a class="btn-primary" href="/participant/default/apply/13/1" data-method="post">Confirm!</a>
But on execution http request is now sent as GET instead of POST.
BEFORE: "POST /participant/default/apply/13/1 HTTP/1.1"
NOW: "GET /participant/default/apply/13/1 HTTP/1.1"
I cannot figure out why this changed and how to get the code to work as POST in new location. This href execution must depend on some additional factor that I am not aware of.
You can send POST request using link thanks to JavaScript inside yii.js file that wraps it in form silently. If this JS is not loaded in assets link works in standard way which is sending GET requests.
Check if yii.js is loaded (usually through registering yii\web\YiiAsset directly or by dependency).

Is there any way to show all the components data from /jcr:content/par/ location

I have a query regarding the data rendering of the different page at one place. As every page is build using many components and all the components data gets stored under jcr location of page ie. /jcr:content/par/{components list}. The data is properly rendering on this page.
Now I have a situation where I need to create a component to search the page(i.e unique product), if this product page is available in the repository, I need to render its data just under the search box. For this I am creating json which I will use to render the content after search found.
But if there is any other way i can include this component from the /par location of the page to just display the data as it is, rather than building json(of all the components data) and then reading it at the time of display.
I am wondering if we have any method to display all the components data by just including the /par/{components} on a page. This way I can speed up the development, and it looks faster way to display the content as well.
thanks in advance....
If you have static page, then you can go through list of search results (product resources) and include component, which renders product, for each of them. Like:
<c:forEach items="${productsList}" var="productPath">
<cq:include path="${productPath}" resourceType="/apps/you-project/components/product-component-name"/>
</c:forEach>
If you show results dynamically - then you can do ajax requests for product resources. Something like this:
var productHtml = CQ.shared.HTTP.get(productPath + ".html");
Or the same using JQuery. Then you can add html to your page.
However, with second approach you should add clientlibs from component /apps/you-project/components/product-component-name to search results page yourself, because they will not be loaded with ajax request.

Understanding wordpress the_content

I have been asked to tweak a friend's website. The website was built for them using Wordpress by an agency they no longer work with. Problem is, I don't know very much about Wordpress, so I have some basic questions…
I can see (by comparing the source as viewed in my browser to the php templates that I can edit through the Wordpress interface) that the elements I need to modify are generated by the page's call to "the_content()". I don't really understand what this function does, but I think it pulls content for a given page from the MySQL database. Is that right?
I suspect that the Wordpress interface alone won't be enough to let me modify elements that come out of that database. Is that correct? How does anyone change, for example, the specific arrangement of text and images on a page if the relevant markup is fetched by "get_content()"?
When developing for WordPress or even just tweaking, it's really important to get at least a general understanding of the "loop" and how we use it for output. In order to output the content you need to call the method within the loop. If it's not within the loop it won't get inserted into your page.
(It works this way with all of your pages... index.php, page.php, single.php, post.php, etc... Your methods need to be placed within the loop in order for them to display within that page.)
Here's a simple example of the way the loop is used to output the title and content of posts:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> // loop start
<h1><?php the_title() ;?></h1>
<?php the_content(); ?>
<?php endwhile; else: ?> // loop end
<p>Sorry, this page does not exist</p>
<?php endif; ?>
If you take a look at any standard WordPress theme (take Twenty Fourteen, for example), you'll find that the_content() is output together with calls to other elements (in this case - the_title()). If you want to want to change the arrangement of the specific elements within your page, just modify the order as they are within your loop, or check the WordPress Codex for additional options/methods.
Here's a short list of some other things you can display:
next_post_link – Displays a link to the post published chronologically after the current post.
previous_post_link – Displays a link to the post published chronologically before the
current post.
the_category – Displays the category or categories associated to the post or page being viewed.
the_author – Displays the author of the post or page.
the_content – Displays the main content for a post or page.
the_excerpt – Displays the first 55 words of a post’s main content then with a [...] or read more link that goes to the full post. The length of excerpts can be controlled by using this slightly advanced method or by using the Excerpt field on the post edit page.
the_ID – Displays the ID for the post or page.
the_meta – Used to display custom fields.
the_shortlink – Displays a link to the page or post using the url of the site using
the ID of the post or page.
the_tags – Displays the tag or tags associated with the post.
the_title – Displays the title of the post or page.
the_time – Displays the time or date for the post or page. This can be customized using the standard php date function formatting.
If you want to further customize some of these methods, you can do that within functions.php. Otherwise, all styling is done within your styles.css.
Probably your content is being generated by a shortcode.
You can modify the content that wordpress fetches from the database with the_content filter:
// returns the content of $GLOBALS['post']
// if the page is called 'debug'
function my_the_content_filter($content) {
// assuming you have created a page/post entitled 'debug'
if ($GLOBALS['post']->post_name == 'debug') {
return var_export($GLOBALS['post'], TRUE );
}
// otherwise returns the database content
return $content;
}
add_filter( 'the_content', 'my_the_content_filter' );
maybe its not the best solution but you can modify the content before it gets output.
source