Laravel - passing route parameter to blade - laravel-5

I am using Laravel 5.6, with the default make:auth mechanism.
In the routes/web.php, I would like to add a language middleware as follow:-
Route::prefix('{lang}')->group(function () {
Route::get('password/reset', 'Auth\ForgotPasswordController#showLinkRequestForm')->name('password.request');
});
Now I wish to apply this in blade:-
<a href="{{ route('password.request') }}">
But debugger just say:-
Missing required parameters for [Route: password.request] [URI: {lang}/password/reset].
I believe the blade cannot not get the {lang} from route. How can it be achieved?

Try this
<a href="{{ url('en') }}/password/reset">

When you pass a prefix in the route it means that it is going to be included in the route, so when you say
Route::prefix('{lang}')->group(function () {
Route::get('password/reset' .......
Route::get('foo/bar' .......
it means the route will be like this "http://yourwebsite.com/{lang}/password/reset"
"http://yourwebsite.com/{lang}/foo/bar"
so each time you pass any route that falls in that group will need to have a variable called $lang so the link can be initialized.
so in your case
<a href="{{ route('password.request', ['lang' => 'en']) }}"> //you can change en to your preferred language.
if you want to add a middleware to specific route or route group, it is passed in this way
Route::get('/', function () {
//
})->middleware('first', 'second');
or
Route::group(['middleware' => ['first']], function () {
//
});
if you want to create another language, you can check the below links:
Laravel localization
spatie/laravel-translation-loader

Related

if else condition in routes laravel

I want only user with same name with the url id can access using if condition
Example
User logged on with name jer
He should only access url with /User-Profile/jer
And not access other page /User-Profile/abc that are not equal to his
name
Doing something like Example:
if{id}!=={{auth->name}}
{
Route::get('NoPermission', 'Restriction#index');
}
else
{
Route::get('/User-Profile/{name}/', 'AccountController#index');
}
How can I compare {name} from url to {auth->name} ?
Route
Route::get('/User-Profile/{name}/', 'AccountController#index');
Blade
<a href="/dashboard/User-Profile/{{ Auth::user()->name }}">{{ Auth::user()->name
}}</a>
You can't access Auth like that in your routes, compare it in your AccountController instead:
public function index($name){
if($name != Auth::user->name()) abort(403);
else...
}
In a service provider (Doesn't really matter which one, but it would be clearer if done in the RouteServiceProvider), add a route binding in the boot method as documented in https://laravel.com/docs/6.x/routing#explicit-binding
public function boot()
{
// Declare binding 'name'.
Route::bind('name', function ($name) {
return App\User::where('name', $name)->first() ?? abort(404);
});
}
Then, use that binding in your routes file
// Use binding name and middleware auth to make sure this route can't be accessed by guest users.
Route::get('/User-Profile/{name}/', 'AccountController#index')->middleware('auth')->name('account_profile');
In your blade file, you can do the following
{{-- Make sure the link is only visible for authenticated users https://laravel.com/docs/6.x/blade#if-statements --}}
#auth
<a href="{{ route('account_profile', ['name' => auth()->user()->name]) }}</a>
#endauth
Allow acces to the page , but before showing content ,check if the url path is == to the id name .
Actually, you can check in your routes like this:
Route::get('/profile/{name}', function(String $name) {
if (!Auth::check() || $name !== Auth::user()->name) {
abort(404);
}
return view("view.auth.profile", ['profile => App\Profile::where('user_id', '=', Auth::id())->first()]);
});
However if you use
Route::get('/profile', 'AuthController#profile')->middleware('auth');
and use Auth::user() in your controller to select the correct profile.
The benefit here is that any unauthenticated users will be automatically redirected to your login page, and there's no need to include the name on your profile link.

Route Not Hitting Controller

When I try to create a view for a product, the URL gets built correctly.
http://localhost:8000/product/my-slug
However, I get a 404 page not found and I have no idea why. It's like the controller is not getting called.
Initiation
<a href="{{ route('product.view', $product->slug) }}">
Route
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
Controller
public function view($slug)
{
$product = Product::find($slug);
return view('products.view', compact('product'));
}
View
<h1>{{ $product->name }}</h1>
EDIT
web.php
Route::get('/', 'ProductsController#index')->name('product.index');
Route::get('/products/create', 'ProductsController#create')->name('product.create');
Route::post('/products', 'ProductsController#store')->name('product.store');
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
/*Route::get('/users', 'UsersController');*/
Route::get('/contact', 'PagesController#contact');
Route::get('/about', 'PagesController#about');
Try changing
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
to
Route::get('/product/{slug}', 'ProductsController#view')->name('product.view');
Ref: Laravel Routing

laravel pathing in link

I am using same thing but result is different. In posts.blade.php i can call like this
<a href = "posts/{{post->id}}"edit>Edit</a>
But when i use same thing in rooms.blade.php indisde rooms folder i had to use like this.
<a href = "{{$room->id}}/edit" class = 'btn btn-primary'> Edit</a>
It is really confusing me. Any solution? i want to add link for editing post inside room. I have tried many things but i dont understand.
My Route
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/posts', 'PostsController#posts');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
You can give the name of the route
Foe example:
Route::get('/post/{id}', 'PostController#showPost')->name('post.show');
And then call the blade
Link to Resource {{ $id }}

Cannot use Auth::routes(); outside default routes/web.php

I have a simple app with a Group in RouteServiceProvider..
// Web routes
protected function mapWebRoutes()
{
Route::group(['domain' => 'example.com']), function()
{
Route::middleware('web')
->namespace($this->namespace);
->group(base_path('routes/web.php'));
});
// Match any other domains or subdomains
Route::group(['domain' => '{domain}'], function()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/org.php'));
});
}
In routes/web.php i can call Auth::routes(); fine;
but in routes/org.php i get a missing required parameters from my views that need the named auth routes (made by default laravel)
"Missing required parameters for [Route: login] [URI: login]. (View: .../resources/views/layouts/loginmenu.blade.php) (View: .."
Issue was not the Auth::routes function but the fact that the {domain} wildcard did not set a domain in the routes (as it would without the wildcard)
To use default auth routes naming conventions you must update the views to include the domain parameter next to the name.
example:
{{ route('login') }}
needs to be
{{ route('login', ['domain'=>$domain]) }}
Of couse you must make the Domain variable available in views. The easiest way (instead of passing it around the controller like a maniac is to simply share the domain variable with the view.
In my middleware in the constructor of my Controller i call:
view()->share('domain', $domain);

NotFoundHttpException Laravel

I am very new in learning Laravel. I want to fetch data from a database and show it. I can do it. But I want to use the title (fetched from the database) as a link. but then I get a NotFoundHttpException.
Routes
Route::get('articles', 'ArticleController#index');
Route::get('articles/{id}', 'ArticleController#show');
Controller
class ArticleController extends Controller
{
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id){
$article = Article::find($id);
return view('articles.show', compact('article'));
}
}
View
#extends('new_welcome')
#section('content')
<h1>Articles</h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop
Can someone help me in this case?
Your problem is because of You've "eat" one curly brace (blade engine skips it):
was:
href="{url ('/articles',$article->id)}"
have to be:
href="{{url ('/articles',$article->id)}}"
as You said:
if I click on any single article title then it can not show me the
specific article. But, if I give the URL "homestead.app/articles/2";
so You can see that when You click on link Your browser's address bar becomes:
homestead.app/{url ('/articles',$article->id)}
Because You're beginner so I'll give You advice to not to set direct url in views using url() helper.
Named routes are better if You want to have app that will work properly if in future You decide to change url from: articles to artcls. In this named routes will save You from bulk changing urls in view files.
set name to Your route using 'as' directive that makes Your routing flexible for changes (when You need to change URL so You change only path and keep views unchanged):
Route::get('articles/{id}', ['as' => 'article', 'uses' => 'ArticleController#show']);
Route::get('articles', ['as' => 'articles', 'uses' => 'ArticleController#index']);
change Your view file (find route helper in href):
#extends('new_welcome')
#section('content')
<h1> Articles </h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop

Resources