Yeasir Arafat Majumder Yeasir Arafat Majumder - 3 years ago 66
PHP Question

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?

Answer Source

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');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download