Laravel "{slug}" not being replaced, instead being url encoded - php

I've recently embarked on learning Laravel. In doing so I've began completing tutorials from around the web, including from youtube. In one such tutorial I've been developing a very basic blog. I've hit something of an impasse when working with slugs. For some reason, it just doesn't want to work for me!
When the following code is entered in my 'routes.php' file, I have noticed that the braces '{' and '}' are automatically converted to URL encoding and the word "slug" is displayed. Additionally, the '/' following the word posts is nowhere to be seen. This means a link will read as "http:[domain name]/posts/%7Bslug%7D[article name]"
Route::get('/posts/{slug}', [
'as' => 'post-show',
'uses' => 'PostController#getIndex'
]);
The problem is partially resolved if I change the code thus:
Route::get('/posts/{slug?}', [
'as' => 'post-show',
'uses' => 'PostController#getIndex'
]);
The word "slug" is no longer displayed as part of the URL. However, for some reason the slug's preceding '/' is still not visible, as though eaten by the slug in question! It now reads "http:[domain name]/posts**%7Bslug%7D**[article name]".
I'd really appreciate pointing in the right direction if anyone can lend a hand. Thank you.

Based on your comment, the way you're calling the action is incorrect. Try this view code:
#if ($posts->count())
#foreach ($posts as $post)
<article>
<h2>{{ $post->title }}</h2>
{{ Markdown::parse(Str::limit($post->body, 157)) }}
read moreā€¦
</article>
#endforeach
#endif
I am not near a terminal to test this, but it should get you closer. The problems this code addresses are:
To generate a URL using the route name, use route. Your code was using action.
To pass a parameter and fill in the "{slug}", you pass an array as the second argument to route (and action for that matter). Your code was neither passing an array nor passing the value into the function.
You were missing a } after the Markdown::parse, but that may have been a copy & paste error.
If this helped you, remember to up vote and mark as accepted!

Related

Laravel Mail not rendering properly

I have the issue in which when I am writing the components in Laravel Mailable, according to their 6.x documentation (since my Laravel version is 6.0). But despite that, in the actual email some components render properly, some are just plain HTML. By plain HTML I mean the text literally says <a>...</a>, but doesn't render the actual element.
Why would that be? Maybe there is a bug, or I am missing something? I have written the most regular email with the components provided by Laravel's docs. There is no linter errors. The view is written in blade btw.
#section('content')
#foreach ($paragraphs as $item)
<p> {{ $item }} </p>
#endforeach
#component('mail::button', [ 'url' => $link ])
{{ $buttonText }}
#endcomponent
#endsection
I resolved the issue. The issue disappeared when I removed any possible indentation in the file.
When every single line started at column 0, the components rendered properly.
Documentation (at least 6.x) doesn't say anything about that, so I assume this is a bug with blade/php mailing compilation or something.

how show text form contact.blade.php in the message in laravel

I makes website by laravel as multi language
and I make full switches to change the language and it work as the flowing code
<?php
return [
'home_menu'=>'home',
'about_menu'=>'about',
'contact_menu'=>'contact',
'message'=>'<h1> welcoome</h1>',
];
Now I need to show text from contact.blade.php from resource/views Instead of
welcoome.
There are many ways you can show your translations in Laravel
{{ trans(filename.arrayIndex) }}
#lang(filename.arrayIndex)
and in case you have html in your translations then
{!! trans(filename.arrayIndex) !!}
I hope this will help you but you must check all the Laravel documentation before starting the project. It will really help you understanding the Lara Concept better

Laravel 5.5: Blade forces https on external URLs

i'm displaying external Website Links from my Users in a Profile Page. The URL Format is: www.xyz.com. When i put a http:// before the {{ url }} Laravel changes the Protocoll automatically to https://. In the Result a lot of the Websites don't open properly. I set the APP_URL in the .env File to
https://domain because i want to use SSL in the App.
Laravel (Blade) is removing the http:// from the URL when i just want to print the Variable too. Even when i put http:// right in front the URL in the DB (MySQL) i still have the same problem.
All i do in the Controller is:
$user = \App\User::find($id);
After 2 days of research i'm reaching out to the community. Basically i'm searching for something like secure_url() inverse.
What i tried:
{{ url('http://'.$url) }}
{!! url('http://'.$url) !!}
{!! url(e('http://'.$url)) !!}
<a href="http://{{ $url }}">
{{ $url }}
Putting the Protocoll right in the DB
What do i not get here? Thank you for any help.
EDIT
Laravel does this only when i visit my site over https.
UPDATE & SOLUTION
It was the problem of the Laravel Cache Speed Package. I deactivated it and everything was back to normal. My fault, my bad.

Laravel 4 Url Helper functions not matching protocol

I had read somewhere the the Laravel helper functions route() and url() would match the protocol of the current page. This has not been my experience and I cannot find a way to do it cleanly.
<!-- https://domain.com/public/route1 -->
<a href='{{ route('route2') }}'> domain.com/route2 </a>
404 due to lack of "/public/" and no https
<a href='{{ url('/route2') }}'> domain.com/public/route2 </a>
proper path but still drops https
<a href='{{ secure_url('/route2') }}> https://domain.com/public/route2 </a>
proper path and protocol but is explicit
Is there a way to do what I want? It seems absurd to me that these helpers would be unable to accomplish something this simple. Something that the following line of code does with no tricks (its downside being the obvious loss of benefits associated with using helper functions [UPDATE: in particular having everything break when running locally due to a different app path]):
<a href='/public/route2'> https://domain.com/public/route2 </a>
To further convolute things I would like to add that when running on localhost, the first version {{ route('route2') }} produces a path that includes /public/, though it is possible that I have a different config file somewhere on the server (this is a very large project). Bonus points if someone can point me in the right direction for that issue as well.
routes.php:
Route::group(array('prefix' => 'route2' ), function(){
Route::get('/', array(
'as' => 'route2',
function(){
return View::make('pages.route2');
}
));
...
});
Thank you kind souls!
UPDATE:
I have continued searching and been unable to find any relevant information regarding Helper function url generation. It seems the majority of info with similar keywords is about forcing HTTPS site-wide (server config) or setting up redirects. It just seems so much simpler to generate the correct link in the first place. Plain HTML can do it, so should Laravel.
UPDATE 2:
Using the asset helper seems to work how I would expect url to. Please someone tell me this hack is not my only option...

How does pagination work in Laravel 5?

I created pagination by writing
$users = User::paginate(25);
Then to display the links, I used the following command,
{!! $users->render() !!}
As the command given below no longer works in Laravel 5
{{$users->links()}}
{!! $users->render() !!} displays the pagination links in the view but there is a slight issue with the links.
This link it generates is like this
http://localhost/laravel/public/users/?page=2
How can I change the generated link to something like this
http://localhost/laravel/public/users?page=2
i.e to remove the slash(/) after users in the url?
The reason for this is that the url with slash after users fails.
Any help regarding pagination will be greatly appreciated.
Thanks
Please try the following $users->setPath('')->render();. I had a similar situation and this fixed the issue for me.

Categories