In my Laravel application, I store a new user via Ajax to the DB. The app always calls the index method. What's wrong?
When I remove the Route::post('/users', 'Admin\UserController#store'); route there is a 405 error. That's correct. But why doesn't it go to the store method?
Controller
<?php
class UserController extends Controller
{
public function index()
{
return view('admin.user.index');
}
public function create()
{
//
}
public function store(UserCreateRequest $request)
{
$user = User::createFromRequest($request);
return response()->json(["id" => $user->id]);
}
}
Routes
Route::group(['prefix' => 'admin', 'as' => 'admin.', ], function () {
Route::get('/users/{user}', 'Admin\UserController#show')->name('users.show');
Route::post('/users', 'Admin\UserController#store');
Route::put('/users/{id}', 'Admin\UserController#updateFromDatatable');
Route::delete('/users/{id}', 'Admin\UserController#destroy');
Route::get('/users', 'Admin\UserController#index')->name('users.index');
Related
I am using Laravel 8, and my application is working fine locally. Now I upload the app to a dedicated server and I'm getting the error below:
BadMethodCallException Method
App\Http\Controllers\User\SendMoneyController::index does not exist.
However, this method exists and it's working fine on the local server.
Controller
class SendMoneyController extends Controller
{
use AmlRulesVerification;
protected $sendMoneyInterface, $transactionInterface, $recipientInterface, $cardInterface, $homeInterface;
public function __construct(
SendMoneyInterface $sendMoneyInterface,
TransactionInterface $transactionInterface,
RecipientInterface $recipientInterface,
CardInterface $cardInterface,
HomeInterface $homeInterface
) {
$this->sendMoneyInterface = $sendMoneyInterface;
$this->transactionInterface = $transactionInterface;
$this->recipientInterface = $recipientInterface;
$this->cardInterface = $cardInterface;
$this->homeInterface = $homeInterface;
}
public function index(Request $request, $id = null)
{
$transaction = $this->sendMoneyInterface->getTransaction($request, $id);
if (isset($transaction['transaction']->card) && $transaction['transaction']->card) {
return redirect()->route('send.money.confirmation', ['id' => $id]);
}
return view('customers.send_money.index',
[
'data' => $transaction,
'countries' => $this->homeInterface->landingViewData($request)
]);
}
}
web.php
Route::group(['middleware'=>'languageSwitcher'],function () {
Auth::routes(['verify' => true]);
Route::get('/', [HomeController::class, 'landingView'])->name('landing.view');
Route::get('users/registration', [UserController::class, 'showRegisterForm'])->name('user.registration');
Route::get('localization/change/language', [LanguageSwitcher::class, 'changeLanguage'])->name('LangChange');
Route::post('/set/email/session', [UserController::class, 'setEmailInSession']);
Route::group(['middleware'=>'auth'],function () {
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/send/money', [UserSendMoneyController::class, 'index'])->name('send.money.index');
)};
)};
Please Verify the namespace in the UserSendMoneyController
Route::get('/send/money', [User\UserSendMoneyController::class, 'index'])->name('send.money.index');
try these changes if commented this line then uncomment too In "app\provider\RouteServiceProvider"
protected $namespace = 'App\\Http\\Controllers';
Why are you using a variable whose value you have already set to null?
public function index(Request $request)
{
$transaction = $this->sendMoneyInterface->getTransaction($request);
if (isset($transaction['transaction']->card) && $transaction['transaction']->card) {
return redirect()->route('send.money.confirmation');
}
return view('customers.send_money.index',
[
'data' => $transaction,
'countries' => $this->homeInterface->landingViewData($request)
]);
}
In Laravel 5.8 making backend rest api app with resource defined in routes/api.php, as
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'adminarea', 'as' => 'adminarea.'], function ($router) {
...
Route::resource('skills', 'API\Admin\SkillController');
now on clients part for listing of skills I need to add current_page and filter_name.
Can it be done with Route::resource definition ?
Actually when we making the resource controller in laravel then it created the method index
create,
store,
show,
edit,
update,
destroy.
method and routes of these respectively but when you are going to define the new method and for access this method then you need to create new route for this.
otherwise it is not possible with the resource route in laravel.
public function abc(Request $request)
{
dd($request->all());
}
route for this
route::post('/abc','ABCController#abc')->name('abc');
you can check the route by this command
php artisan route:list or
php artisan r:l
hope you understand.
After some search I found a decision in defining in routes one more post route:
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'adminarea', 'as' => 'adminarea.'], function ($router) {
Route::post('skills-filter', 'API\Admin\SkillController#filter');
Route::resource('skills', 'API\Admin\SkillController');
...
and in app/Http/Controllers/API/Admin/SkillController.php :
<?php
namespace App\Http\Controllers\API\Admin;
use Auth;
use DB;
use Validator;
use App\User;
use App\Http\Resources\Admin\Skill as SkillResource;
class SkillController extends Controller
{
private $requestData;
private $page;
private $filter_name;
private $order_by;
private $order_direction;
public function __construct()
{
$this->middleware('jwt.auth', ['except' => []]);
$request = request();
$this->requestData = $request->all();
}
public function filter()
{
if ( ! $this->checkUsersGroups([ACCESS_ROLE_ADMIN])) {
return response()->json(['error' => 'Unauthorized'], 401);
}
$backend_items_per_page = Settings::getValue('backend_items_per_page');
$this->page = !empty($this->requestData['page']) ? $this->requestData['page'] : '';
$this->filter_name = !empty($this->requestData['filter_name']) ? $this->requestData['filter_name'] : '';
$this->order_by = !empty($this->requestData['order_by']) ? $this->requestData['order_by'] : 'name';
$this->order_direction = !empty($this->requestData['order_direction']) ? $this->requestData['order_direction'] : 'asc';
$this->index();
$skills = Skill
::getByName($this->filter_name)
->orderBy($this->order_by, $this->order_direction)
->paginate($backend_items_per_page);
return SkillResource::collection($skills);
} // public function filter()
public function index()
{
if ( ! $this->checkUsersGroups([ACCESS_ROLE_ADMIN])) {
return response()->json(['error' => 'Unauthorized'], 401);
}
$backend_items_per_page = Settings::getValue('backend_items_per_page');
$skills = Skill::paginate($backend_items_per_page);
return SkillResource::collection($skills);
}
As that is adminarea some filter actions can have more 1 filter...
Just interested to know which other decisions are possible here?
I am currently trying to build a user registration system with edit fields. At the edit portion, I had to create separate views for editing/updating personal details, email, and passwords.
I started with an empty resource controller. it had only one edit method. Hence I added additional edit methods. Each method can have a separate route. However, I have a hard time having a separate route for each update method in each section as the resource has only one route like this in docs:
PUT/PATCH /photos/{photo} update photos.update
Is there any workaround for this?
Controller
class UserController extends Controller
{
public function __construct()
{
$this->middleware(['auth', 'verified']);
}
public function index()
{
return view('users.index');
}
public function edit_personal(User $user)
{
$user_profile = User::find($user->id);
return view('users.edit.personal', ['users' => $user_profile]);
}
public function update_personal(Request $request, User $user)
{
// How to write route for this method.
}
public function edit_email(User $user)
{
$user_profile = User::find($user->id);
return view('users.edit.email', ['users' => $user_profile]);
}
public function update_email(Request $request, User $user)
{
// How to write route for this method.
}
public function edit_password(User $user)
{
$user_profile = User::find($user->id);
return view('users.edit.password', ['users' => $user_profile]);
}
}
Routes
Auth::routes(['verify' => true]);
Route::get('/', function () {
return view('welcome');
});
Route::get('/users/{user}/personal', 'UserController#edit_personal')->name('users.personal');
Route::get('/users/{user}/email', 'UserController#edit_email')->name('users.email');
Route::get('/users/{user}/password', 'UserController#edit_password')->name('users.password');
Route::resource('users', 'UserController');
Basically I have separated edit portion of user controller into personal, email and password sections and they have separate forms. I want to write update functions for each section in UserController.
don't know why are you using separate forms for updating each fields while you can do it in a single form. however you can use either put/patch or post method for updates. here's i am using post for example.
routes:
Route::get('users/{user}/personal', 'UserController#edit_personal')->name('users.personal');
Route::post('users/{user}/personal', 'UserController#update_personal')->name('users.update-personal');
Route::get('users/{user}/email', 'UserController#edit_email')->name('users.email');
Route::post('users/{user}/email', 'UserController#update_email')->name('users.update-email');
Route::get('users/{user}/password', 'UserController#edit_password')->name('users.password');
Route::post('users/{user}/password', 'UserController#update_password')->name('users.update-password');
as you are using route model binding you can directly get the object.
public function edit_personal(User $user)
{
return view('users.edit.personal', ['users' => $user]);
}
public function update_personal(Request $request, User $user)
{
//validation goes here
$user->update([
'value' => $request->value,
...........
]);
}
I use JWT authentication for my laravel api middleware group. Here is my route configuration:
Route::group(['middleware' => ['api']], function() {
Route::post('login', 'AuthController#login');
Route::post('test', 'AuthController#test');
});
This code works well and I can't have unauthorized access to test method of my AuthController class:
class AuthController extends Controller
{
public function __construct()
{
$this->middleware(['jwt.auth'])->except('login');
}
public function login()
{
//...
}
public function test()
{
//...
}
}
but when I change the route config to identify controller's methods name dynamically (as you see in the following code snippet), the authentication does not work anymore and I can access test method without bearer token!
Route::group(['middleware' => ['api']], function() {
Route::post('/{controller}/{method}', function ($controller, $method) {
$controllerClass = 'App\\Http\\Controllers\\'.$controller.'Controller';
if(method_exists($controllerClass, $method))
{
$controller = App::make($controllerClass);
return $controller->callAction($method, array());
}
return abort(404);
});
});
Any idea?
How I can make a internal request without change the current route.
My routes:
Route::get('/my/action1', ['as' => 'action1', 'uses' => 'MyController#action1']);
Route::get('/my/action2', ['as' => 'action2', 'uses' => 'MyController#action2']);
My controller:
namespace App\Http\Controllers;
use Route;
use Request;
class MyController extends Controller
{
public function action1()
{
return 'action1';
}
public function action2()
{
$request = Request::create('/my/action1', 'GET');
$response = app()->handle($request);
dump(Route::getCurrentRoute()->getName()); // I need return "action2"
return $response; // I need return "action1"
}
}
When the internal request is called, the current route is changed and I don't want this effect. What I need make?