laravel 5 slug will not return pages - php

I try to get my route without any additional parameter in url and i'm getting Sorry, the page you are looking for could not be found. error.
If I use url like: domain/category/lenovo where lenovo is my slug it's working. But if I try to get my url like: domain/lenovo I'm gettin error above.
here is my codes:
route
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
function
public function totalcategoriessubs($categoryslug) {
$categories = Category::where('slug','=',$categoryslug)->with('subcategories')->paginate(12);
return view('front.categoriessubs', compact('categories'));
}
it also brakes my other urls such as domain/admin/dashboard etc.
any idea?

It seems that is caused by the order of your routes.
Your actual problem is that when you hit a fixed endpoint, for example /dashboard this will be handled by Route::get('/{categoryslug}', ...) before the correct endpoint (Route::get('dashboard', ...)). So, as your system notices that there is no category with that slug (dashboard) it throws the error.
Try change the order of your routes, like this:
Route::get('/category/{categorySlug}', 'CategoryController#index');
Route::get('/dashboard', 'DashboardController#home');
// and so on..
Route::get('/{categoryslug}', 'frontend\FrontendController#totalcategoriessubs')
->name('catwithsubs')->where('categoryslug', '[\w\d\-\_]+');
Always put the routes that have fixed parts (.../category/...)of the route before the ones that have dynamic elements (.../{categoryslug}/).

Related

Why does my edit & delete methods return a "404 not found" when my index & create routes work

Laravel version has updated and the routes is now expecting an object instead of an id from when i last used it.
My Routes:
When I try to pass over the $item object which the method in the controller wants. I get a 404 not found and my logs aren't returning... meaning the function isn't running. When the $item obj is not passed over the function realizes that a parameter is missing thus the method is recognized by the blade as being the same as the one in the controller.
Calling the edit function in Blade:
Controller Code:
I appreciate any help whatsoever.
The order of your routes is probably wrong
when you first define the show route with /item/{item} and then create with /item/create laravel will think the "create" is the id (or reference)
best way is to have
the index
create
....
show
Code Example correct
Route::get('/', ProductIndex::class)->name('product.index');
Route::get('/new', ProductCreate::class)->name('product.create');
Route::get('/{product}', ProductShow::class)->name('product.show');
Code Example wrong
Route::get('/', ProductIndex::class)->name('product.index');
Route::get('/{product}', ProductShow::class)->name('product.show');
Route::get('/new', ProductCreate::class)->name('product.create');

laravel 6 route returing 404 not found

While Passing variable in URL and route returning only 404, not found#.
Did some google and found some result and tried some methods on youtube its also not working. Totally Beginner to laravel
room.blade.php
//In roomdetail->id its passing the value of room
Detail
web.php
//The route which I am using
Route::get('/detail/{$id}', 'DetailController#show')->name('detail.show');
DetailController.php
public function show($id)
{
dd($id);
}
}
In DD I want to get the Id so I can fetch data from the database it's showing only 404
Your route is wrong you are using $ in a param which is wrong instead of {$id} in route it should be {id}
change this
Route::get('/detail/{$id}', 'DetailController#show')->name('detail.show');
To
Route::get('/detail/{id}', 'DetailController#show')->name('detail.show');
and a quick tip as you are using named route then it would be better if you use route method to pass variable to route in view
So in your view change this:
Detail
To
Detail
Thanks
Remove '/' from URL or pass route
Detail
OR
Detail
or change your route as {id} instead of {$id}
it should be Route::get('/detail/{id}', 'DetailController#show')->name('detail.show');

Limit parameters in codeigniter URI

I'm facing an issue with codeigniter URI routing.
I have a route like this:
website.local/terms/SOMEINFORMATIONID/SOMECURRENCY
Where SOMEINFORMATIONID & SOMECURRENCY are the URI parameters.
The problem is, when I call website.local/terms/SOMEINFORMATIONID/SOMECURRENCY/SDFSDFSDFSD
So this means, that i'm introducing a third parameter, the URI still works, but what I want is, when the route is "executed" with more than 2 parameters, get a 404 error.
I was trying to find something in codeigniter documentation, but I couldn't find anything, anyway I don't know for what I should look for (codeigniter uri parameters limited, etc...).
As additional information, my route looks like this in routes.php:
$route['terms/(:any)/(:any)'] = 'xxx/terms/$1/$2';
in controller:
//check segment
$sdf= $this->uri->segment(4);
if ( ! empty($sdf)) $this->my404();
and function
public function my404 () {
$this->output->set_status_header('404');
$data['error'] = 'Not Found...'
$this->smarty->view( 'error.tpl', $data);
}
if you are using ci3, you can add one more route at last like
$ route ['(. +)'] = 'error_page'; // all the urlĀ“s that you don't have before will display the error you want

Controller returning application/json instead of view

I have a very weird problem. I could access the page and everything was fine until i added a few new routes in my web.php routing file. Problem is with 5th route(named post.create). The ** are just to highlight the line/route i am talking about:
Route::group(['prefix'=>'admin', 'middleware'=>'auth'], function()
{
Route::get('home', 'HomeController#index')->name('admin.home');
Route::get('post/all','PostsController#index')->name("post.all");
Route::get('post/{id?}','PostsController#show')->name('post.fetch');
**Route::get('post/create','PostsController#create')->name('post.create');**
Route::post('post/store', 'PostsController#store')->name('post.store');
Route::put('post/{id?}','PostsController#update')->name('post.update');
Route::delete('post/delete/{id}','PostsController#destroy')->name('post.delete');
Route::get('category/create','CategoriesController#create')->name('category.create');
Route::post('category/store','CategoriesController#store')->name('category.store');
Route::get('category/all','CategoriesController#index')->name('category.all');
Route::get('category/{id?}','CategoriesController#show')->name('category.fetch');
Route::delete('category/delete/{id}','CategoriesController#destroy')->name('category.delete');
Route::put('category/{id}','CategoriesController#update')->name('category.update');
});
When i am accessing this route i get a blank page with a pair of curly braces only, nothing else. There is a message on the browser console that says - Resource interpreted as Document but transferred with MIME type application/json.
But if i change the route to
Route::get('posts/create','PostsController#create')->name('post.create');
,which is just add an additional s, i get the full view of the page.
I cannot seem to figure out why the earlier route is sending back application/json(seems an empty object). I made no change to the controller function. Here is the code for the PostsController#create function:
public function create()
{
$categories = Category::all();
return view('admin.posts.create', compact('categories'));
}
I have tried to return a different view or a simple string from this function for this route. Nothing seems to work.
What am i doing wrong, can anyone please help?
Laravel will serve the first route matched in the order you define them. Since you have post.fetch first it is serving that route with 'create' as the id parameter.
In your routes file place post.create before post.fetch so you have:
Route::get('post/create','PostsController#create')->name('post.create');
Route::get('post/{id?}','PostsController#show')->name('post.fetch');
Route::post('post/store', 'PostsController#store')->name('post.store');
Route::put('post/{id?}','PostsController#update')->name('post.update');
You should name Blade file as:
resources/views/admin/posts/create.blade.php
Blade view files use the .blade.php file extension and are typically stored in the resources/views directory
https://laravel.com/docs/5.4/blade#introduction
Update
In comments I've recommended you to move the route before the 'post/{id?}'.

Only allow URL's specified in routes to be seen in Codeigniter

If I have a controller called articles, which has a method called view_articles, a user can type in http://example.com/articles/view_articles/some-post and have it return a page.
I have specified a route to be http://example.com/article/post-name. How can I make it so that only the URL specified in the route is visible? Is there a way for articles/view_articles/some-post to show a 404 instead of showing the same page as the route URL?
I am trying to prevent duplication for SEO purposes.
You can always make default routing to a 404 page by correctly defining routes in your routes.php file:
$routes['article/(:any)'] = 'articles/view_articles/$1';
$routes['(:any)'] = 'main/e404';
As stated by CodeIgniter user guide:
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
So you can basically define all you want to be seen at the beginning of the file and block everything else on the last line.
As for your main(can be any other) controller's 404 method -
function e404() {
show_404();
}
Use $this->uri->segment(n) as part of the URI class inside of view_articles to redirect traffic to your route if the URI contains view_articles as it's second segment.
I'v done that in other tricky way, first of all you should add some code to __construct function in sys/core/Controller.php
you can check whether the requested url is in routes or not by this code
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}

Categories