I am a beginner in laravel framework, and while i am building a simple api routes i have faced a problem which is : I have created in api.php
Route::resource('userss','User\UserController');
User controller has standard api methods (index,store, show etc..) after that i listed the routes with this command :
php artisan list:route
and i have got the results as in the below image
route/api.php
use Illuminate\Http\Request;
Route::resource('userss','User\UserController');
UserController.php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;
class UserController extends Controller
{
public $successStatus = 200;
public $appNameToken = 'stiskAppNameToken';
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$users = User::all();
return response()->json(['data'=> $users],200);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$rules = [
'name' => 'required|min:4',
'username' => 'required|min:4|unique:users',
'email' => 'required|email|unique:users',
'password' => 'required|min:4|confirmed',
'status' => 'required',
'dept_id' => 'required'
];
$this->validate($request,$rules);
$data = $request->all();
$data['password'] = bcrypt($request->password);
$data['verified'] = User::USER_VERIFIED;
$data['verification_token'] = User::generateVerificationCode();
$user = User::create($data);
return response()->json(['data' => $user],201);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
$user = User::findOrFail($id);
return response()->json(['data'=>$user],200);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
But the problem now is every time i make a request from postman to mylocal.test/userss/
with different methods (PUT,POST,GET etc..)
i got a list of the users ( index methods executed!)
what is the problem? why all methods requests are going to execute @index method body.
I already handled MethodNotAllowedHttpException
in render function but no way
is suppose that Route::resource('userss','User\UserController');
handling such that problem?
and route my request to a correct method in my controller?
Update: The problem i think was in postman or my local web server Also after using Authentication you will not face this problem thanks for you all
Route::resource
does is register those routes you are showing, that is all – lagboxweb.php
content might help more. And the controller code won't hurt to be shared :) – nakov