Controller returning application/json instead of view - php

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?}'.

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 7: Generate named view-routes from blade files in directory

Is there a nice way, to solve this issue: I have a folder ressources/views/project/content with several blade teplates, let's say:
home.blade.php
how-to.blade.php
info.blade.php
best-way-to-score.blade.php
...
Right now, I define one view route per file:
Route::view('/home', 'project.content.home')->name('home');
Route::view('/how-to', 'project.content.how-to')->name('how-to');
...
How can I create these routes on thy fly? I could solve it with a loop through all files in this directory, but maybe there is a more elegant way/function in laravel I don't know yet?
If I understand correctly, what you you need is a generic get route like this:
Route::get('/{page}', 'PageController#show');
and then you need a PageController with a function to return the requested page:
public function show($page)
{
return view('project.content.'.$page);
}
Just have in mind that this kind of route will "catch" every get request so put it at the end of the web.php file

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

Laravel routes not working inside a prefix unless given a variable

I'm fairly new to Laravel. I'm having a problem with routing.
Route::group(['prefix'=>'api/v1'],function(){
Route::resource('results','RequestController');
Route::get('results/getByName/{name}','RequestController#getByName');
Route::get('results/getLastTen','RequestController#getLastTen');
});
The problem is that the last route under prefix api/v1 does not work. When I call it it shows nothing, not even any error.
The code at the requestController is:
public function getLastTen(){
$results=DB::table('latest_random_trends')->limit(10)->get();
return $results;
}
Everything is alright with the code on the controller since it works when I call it from the routes.php file outside of the prefix 'api/v1' like this:
Route::get('results/getLastTen','RequestController#getLastTen');
but when it is inside the prefix it does not work unless I add a variable to it like this:
Route::get('results/getLastTen/{var}','RequestController#getLastTen');
Since you have a Route::resource above it, I think what's happening is that the show method on Resource Controller is getting the route instead of the one you wrote.
Try one the following:
Exclude the show method if you're not going to use it
Route::resource('results','RequestController', ['except' => 'show']);
Move your custom route above the resource route
Route::group(['prefix'=>'api/v1'],function(){
Route::get('results/getLastTen','RequestController#getLastTen');
Route::resource('results','RequestController');
Route::get('results/getByName/{name}', 'RequestController#getByName');
});
For more information, check out the show action on Laravel Docs

Laravel 5.1 routing returning wrong content from controller

I have this really weird problem with laravels routing.
I started to make some routes and controllers and just returning strings from each controller confirm that it worked.
And everything did work.
Now when i started making the master view and putting it together with some templates for the routes I noticed that the string that laravel returns isn't the string i wrote.
All routes return "This is routename page"
The only routes that actually work as expected is the routes with wild cards, and the route going to the start page.
Those routes return the correct strings.
Example routing
Route::get('/users', 'UserController#index');
class UserController extends BaseController {
public function index() {
return 'List of users!';
}
});
This routing displays "This is user page" (NO ERROR)
I have tried returning the string directly from the route, clearing all the cache files i could find including route cache, restarting browser and MAMP
Just to be clear, the routing returned the correct strings when I made the route.
I have installed Elixir to compile my scss files, but i doubt that should have anything to do with my problem.. :(
Figured it out just after I posted the question!
I had a route with a wildcard directly after the root
Route::get('/{'user'});
This route were overriding all other routes that only had one parameter after the root. So if I go to the url "/users" the route will assume it is a wildcard and send it to another controller that returns the string "This is {wildcard} page!", Brainfreez! :P

Categories