Laravel: Cannot route to expected URL - php

Here is my nav-bar:
<div class="col-md-2">
<ul class="list-group-item">
<li><i class="fa fa-fw fa-file</i> All Post
</li>
<li><i class="fa fa-fw fa-plus-circle"></i> Create New Post</li>
<li><i class="fa fa-fw fa-tasks"></i> Manage Posts</li>
</ul>
</div>
and here is my route.php
Route::group(['prefix' => 'posts'], function(){
Route::get('', 'PostController#index');
Route::get('create', 'PostController#create');
Route::post('confirm', 'PostController#confirmation');
Route::get('{postID}', 'PostController#show');
Route::get('posts/manage', 'PostController#manage');});
I expect when I click on the "Manage Posts" button, it will redirect me to function manage() in my PostController.
But when I click on it, it redirects to a view which belongs to storage/framework/views which is show() in my PostController.
I don't know why and how to make it to the right url.
Can somebody help me with this one please?
Thank you.

First of all, your link links to /posts/management, not /posts/manage. Second, you already have the prefix posts for this route-group, so the route posts/manage will be available under the url /posts/posts/manage.
You also want to move the manage route before your {postID} route, because {postID} will just catch anything, so the router has to first check the manage-route, and only if it doesn't match, the catch-all route.
And you should control what is accepted as a valid postID using Route Parameters: Regular Expression Constraints.
Route::get('{postID}', 'PostController#show')->where('id', '[0-9]+');

Related

Laravel, get id from an <a> link

How to get user id properly?, This is error message Missing required parameter for [Route: user.currentRequest] [URI: user/user-current-request/{user}] [Missing parameter: user]
An User has many relationship with Request, and the url will be user-current-request{user-id}
//layouts.app
<li class="nav-item">
<a class="nav-link" href="{{ route('user.currentRequest') }}">Current Request</a>
</li>
Route::group(['prefix'=>'user', 'middleware'=>['isUser','auth']], function(){
Route::get('user-current-request/{user}', [UserController::class, 'currentRequest'])->name('user.currentRequest');
});
function currentRequest(User $user)
{
dd($user);
return view('dashboards.users.currentRequest');
}
Your route in Layouts.app need params.
<li class="nav-item">
<a class="nav-link" href="{{ route('user.currentRequest', $user) }}">Current Request</a>
</li>
If you want to give this params in layout you have to do it in the providers / appserviceprovider

Route [transaksi.index] is not defined. but the route already exists with route:resource

I don't know why the error is being thrown, because the route already exists.
master.blade.php:
<!-- Nav Item - Transaksi -->
<li class="nav-item">
<a class="nav-link" href="{{route('transaksi.index')}}">
<i class="fas fa-fw fa-folder"></i>
<span>Transaksi</span></a>
</li>
web.blade.php:
Route::resource('transaksi','TransaksiController');
TransaksiController:
public function index()
{
$data = DB::table('tbl_transaksi')
->where('tbl_transaksi.nama_peminjam','like',"%{$request->keyword}%")
->paginate(20);
return view('admin.transaksi.index',['data'=>$data]);
}
The error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [transaksi.index] not defined. (View: C:\xampp\htdocs\SistemPerpustakaan\resources\views\admin\master.blade.php)
http://localhost:8000/dashboard
resolved, I just forgot to delete the route with the same name

How can I call a page when i clicked the button in codeiginiter?

I am not calling a page in codeiginiter when i click button..
i am new in this so please HELP....
<i class="fa fa-check-square-o nav_icon"></i>Forms<span class="fa arrow"></span>
<ul class="nav nav-second-level collapse">
<li>
Inputs
</li>
<li>
Form Validation
</li>
</ul>
on theme implement in codeiginiter i can't call the page
it's is show the page was not found but i given correct path on it.
First you have to understand MVC Architecture..
You just have to call Controller/Method where you call your view.
example:
Controller
<?php
class Mycontroller extends CI_Controller{
public function orders(){
$this->load->view('orderpage');
}
}
?>
view - Orderpage.php
<body>
<h3>orders list</h3>
</body>
your just have to add anchor tag like
Go to orders
Edit config/config.php file
$config['base_url'] = 'http://' . $_SERVER['HTTP_HOST'] . '/codeigniter/';
in view
Inputs
you need to setup routs for View
go to
\application\config\routes.php
and add a route at the end like this
$route['ViewName'] = 'ViewPath';
My views are presented in
application\views\user
so i will add
$route['confirm'] = 'user/confirm';

laravel a href validation

I would like to ask if it is possible to validate a href element in laravel.
For example, I'm having a bunch of people responses in my guest book and each user is able do delete his posts. I'm deleting it like this:
<div class="col-lg-2">
<ul class="list-unstyled trash">
<li>
<a href="/responses/{{$response->id}}">
<span onclick="alertDialog()" class="glyphicon glyphicon-trash"></span></a></li>
<li><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#{{$response->id}}">Edit</button></li>
</ul>
</div>
Unfortunately, every single user can change "/responses/{{$response->id}}"> id via browser developer tools and delete other user's responses. Is there any possible solutions to prevent this issue?
Just check the logged user before rendering that html section:
<div class="col-lg-2">
<!-- assuming post is your variable and user is a property which references the user who created the record -->
#if($post->user == Auth::id())
<ul class="list-unstyled trash">
<li>
<a href="/responses/{{$response->id}}">
<span onclick="alertDialog()" class="glyphicon glyphicon-trash"></span></a></li>
<li><button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#{{$response->id}}">Edit</button></li>
</ul>
#endif
</div>
This way only the user who owns the post will be able to see the button in the html.
You need to use policies to check if the user that tries to delete a record is its owner:
public function delete(User $user, Model $model)
{
return $user->id === $model->user_id;
}
Or you can do it manually in a controller method:
if (auth()->id() === $model->user_id) {
// Delete the record
}
You should make /responses/{{$response->id}} a POST route and verify that the passed id matches with the authenticated users id. ( Auth::user()->id;)
I ended up checking like this in my destroy(id) method:
$count = Comment::where(['response_id' => $id])->count();
if (($responses->where('id',$id)->value('guests_email') == Cookie::get('email') && ($count==0)) || (Auth::check()))

Laravel 5.3 Route in template showing Route Not Defined

i am updating my application from laravel 5.2 to 5.3. Most of the things seems to work fine.
But i dont know what is happening but when i am trying to define route in anchor tag, its not working. I have done something similar to this:
<a href="{{route('backend.pages.index')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Its showing error Route [backend.pages.index] not defined.. Here is how the created the route.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController');
});
I have a template called 'mainmenu.blade.php' in which i have use this route. This mainmenu is called in main structure through #include('layouts.backend.backendstructure.mainmenu').
Is routing method is changed in laravel 5.3? Or is there any mistake from my side?
Thank you!(Advance)
The problem here is
{{route('backend.pages.index')}}
instead use
<a href="{{route('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
The route is defined as backend/pages. To return view add a method in PagesController and return the view there.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController#dummymethod');
});
Dummy method
public function dummymethod
{
return view('backend.pages.index');
}
Edit
I think you're looking for something like this
Route::resource('backend/pages','Backend\PagesController', ['names' => ['index' => 'backend.pages.index']]);
Check the docs here
You should write your code like this:
<a href="{{ route('backend/pages')}} " class="nav-link ">
<span class="title">All Pages</span>
</a>
or like this:
<a href="{{ url('backend/pages') }}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Try:
<a href="/backend/pages" class="nav-link ">
<span class="title">All Pages</span>
</a>
https://laravel.com/docs/5.3/routing
You can try link with URL to like, I use in following manner
<a href="{{URL::to('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>

Categories