Invalid ruote rule to catch request from vue-tables-2 component - laravel

Using vue-tables-2 component in my #vue/cli 4.0.5 app
I see that GET request generated
http://local-ctasks-api.com/api/adminarea/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
and I try in Laravel 6 Backend REST API to set route to catch it as :
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'adminarea', 'as' => 'adminarea.'], function ($router) {
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
But I got 404 error,
Is my route invalid ?
UPDATED # 1:
Yes, var with “/api” was unaccesible. I fixed it and running request without “/adminarea”
http://local-ctasks-api.com/api/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
I moved route definition out of any block :
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
I got error in browser :
"error": "INCORRECT ROUTE"
with control action defined in app/Http/Controllers/API/Admin/ActivityLogController.php :
public function filter( $query, $limit, $ascending, $page, $column )
{
\Log::info('!!++ filter $this->requestData ::');
\Log::info(print_r( $this->requestData, true ));
Why error ?
Thanks!

I think you forgot put api on prefix
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'api/adminarea', 'as' => 'adminarea.'], function ($router) {
EDIT:
Don't put parameter on route like that, use Request instance
Route::get('activity-logs-filter,'API\Admin\ActivityLogController#filter');
and controller
public function filter(Request $request){
$query = $request->query;
$limit = $request->limit;
$ascending = $request->ascending;
$page = $request->page;
$column = $request->column;
dont forget use Illuminate\Http\Request; on your controller

Related

How can I get api/register with Passport in laravel working?

Following a tutorial: https://www.twilio.com/blog/build-secure-api-php-laravel-passport
I manAged to get Laravel/Passport installed formy Laravel Api and Vue application.
I managed to create attoken with:
localhost:8000/oauth/token
get the login working in Postman:
localhost:8000/api/login?email=jennie05#example.com&password=password
Now when I try to register a user I get returned to the home-page.
I do get some "undefined method" errors from VS Code, but they show up in the login method,
and not in the failinf register method:
Here is the controller:
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
class AuthController extends Controller
{
public function register(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|max:55',
'email' => 'email|required|unique:users',
'password' => 'required|confirmed'
]);
$validatedData['password'] = bcrypt($request->password);
$user = User::create($validatedData);
$accessToken = $user->createToken('authToken')->accessToken;
return response([ 'user' => $user, 'access_token' => $accessToken]);
}
public function login(Request $request)
{
$loginData = $request->validate([
'email' => 'email|required',
'password' => 'required'
]);
if (!auth()->attempt($loginData)) {
return response(['message' => 'Invalid Credentials']);
}
$accessToken = auth()->user()->createToken('authToken')->accessToken;
return response(['user' => auth()->user(), 'access_token' => $accessToken]);
}
}
In these are the routes in api.php:
Route::post( 'register', 'App\Http\Controllers\API\AuthController#register');
Route::post( 'login', 'App\Http\Controllers\API\AuthController#login');
// Route::prefix('v2')->group(function(){ // prefix voor versie 2
Route::apiResource('/cards', 'App\Http\Controllers\CardController');
Route::apiResource('/games', 'App\Http\Controllers\GameController');
//Route::get('games', 'App\Http\Controllers\GameController#index')->middleware('auth:api');
//Route::post('games', 'App\Http\Controllers\GameController#store')->middleware('auth:api');
//Route::get('games/{id}', 'App\Http\Controllers\GameController#show')->middleware('auth:api');
//Route::put('games/{id}', 'App\Http\Controllers\GameController#update')->middleware('auth:api');
//Route::delete('games/{id}', 'App\Http\Controllers\GameController#destroy')->middleware('auth:api');
Route::get('/gameByPin/{pin}', 'App\Http\Controllers\GameController#searchPin');
Route::apiResource('/speler', 'App\Http\Controllers\SpelerController');
//});
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
is there anyone who can help troubleshooting this?
Postman does not give me errors, just the redirect to homepage..
Wow,
Thanks Patrick, your the best.
Just checked all the steps in the documentation.
reran the installation command I ran when setting up everything. ( Apperently need to do this twice ?!):
php artisan passport:install
It returned:
Encryption keys already exist. Use the --force option to overwrite them.
Personal access client created successfully.
Client ID: 5
Client secret: ikJEfthxxxxxxxxxxxxxxxxxxxxxxxxxxxBjJ36
Password grant client created successfully.
Client ID: 6
Client secret: H42lpMxxxxxxxxxxxxxxxxxxxxxxxxxxxQGZgH
Now the post request results in a token!
The error VS Code shows d for "use HasApiToken" is:
Undefined type 'Laravel\Passport\HasApiTokens'.intelephense(1009)

Laravel 8: Array to String conversion on route register

Is there any way to define the name of the route group in laravel 8?
I'm trying to build routes for the sellers to go to the order management site, and below is my routes list in web.php
use App\Http\Controllers\Seller\OrderController;
Route::group(['prefix' => 'seller', 'middleware' => 'auth', 'as' => 'seller.', 'namespace' => 'Seller'], function () {
Route::redirect('/','seller/orders');
Route::resource('/orders', [OrderController::class]);
});
Errors
ErrorException
Array to string conversion
at C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:416
412▕ protected function getResourceAction($resource, $controller, $method, $options)
413▕ {
414▕ $name = $this->getResourceRouteName($resource, $method, $options);
415▕
➜ 416▕ $action = ['as' => $name, 'uses' => $controller.'#'.$method];
417▕
418▕ if (isset($options['middleware'])) {
419▕ $action['middleware'] = $options['middleware'];
420▕ }
1 C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:416
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php", ["orders", "index", "orders.index"])
2 C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:189
Illuminate\Routing\ResourceRegistrar::getResourceAction("orders", "index", [])
Change the following line
Route::resource('/orders', [OrderController::class]);
to
Route::resource('/orders', OrderController::class);
The reason it is giving you that error is because you are passing that `OrderController as an array:
Route::resource('/orders', [OrderController::class]);
You are probably confusing the syntax for regular routes with the syntax for resource controllers.
For resource controllers you have to pass the class name as a string:
Route::resource('/orders', OrderController::class);
this worked for me. SO #Iyathurai Iyngaran
Change the following line
use locationcontroller/OrderController as OrderController;
Route::resource('/orders', OrderController::class);

Keep getting "MethodNotAllowedHttpException" when trying to display a page redirected from an external URL

I'm trying to reach to my local page from an external URL, which is from a payment gateway, based on the return URL parameter. However, when I link it back to my page, I keep getting the "MethodNotAllowedHttpException" error. Below are my codes;
Order Controller:
public function kiplestatus(Request $request, $id) {
dd($request);
$showFeedbackForm = true;
$order = \App\Order::where('id', $id)->get();
}
Route:
Route::get('/order/kiplestatus/{id}', [
'uses' => '\App\Http\Controllers\OrderController#kiplestatus',
'as' => 'order.kiplestatus',
'middleware' => ['auth','verify.mobile.no','agent'],
]);
Am I doing anything wrong here?

How can I return two times back in Laravel 4.2?

Which classes to use for caching certain token or url? After login, I want the user to redirect two urls back in Laravel 4.2? I have tried already with URlGenerator and Request class in my User authentification function so I could catch my previous url which is GET request for login, while the desired url is before this request.
My login function:
namespace \Users\Repositories;
use View , Input , Redirect , Config;
use Illuminate\Routing\UrlGenerator;
use Users\Repositories\Contracts\AuthRepositoryInterface;
use Illuminate\Support\MessageBag;
use Illuminate\Http\Request;
class AuthRepository implements AuthRepositoryInterface {
private $messageBag;
private $errors;
private $urlGenerator;
private $request;
public function __construct(MessageBag $messageBag, UrlGenerator $urlGenerator, Request $request) {
$this->messageBag = $messageBag;
$this->urlGenerator = $urlGenerator;
$this->request = $request;
}
public function postLogin() {
$remember_me = (Input::get('remember_me') == 1 ? true : false);
try
{
// Login credentials
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
);
// Authenticate the user
$user = \Sentry::authenticate($credentials, $remember_me);
// giving example
//if(token or url)
//redirect to 'frontend.fee.registration'
//else
return Redirect::route('profile')->with('success' , trans('users::success.login_success'));
}
My routes:
Route::get( '/login' , array( 'as' => 'login' , 'uses' => 'HomeController#userLogin' ) );
Route::post('/login' , array( 'as' => 'postLogin' , 'uses' => '\Users\Controllers\AuthController#postLogin' ) );
Redirect destination:
Route::get('/fee/{id}/register/' , array( 'as' => 'frontend.fee.registration' , 'uses' => 'FeeController#feeRegistration' ) );
There is no built in solution, but you can use session to save current URLs and then use them to go back 2-3-4 etc pages back.
Look at the answer and the code here: How to return back twice in Laravel?

Laravel writing the correct variable to the URL and getting the route to pick it up

So I have these 2 routes:
/*
* Account Activate (GET)
*/
Route::get('/account/activate/{code}', array(
'as' => 'account-activate',
'uses' => 'AccountController#getActivate'
));
/*
* Account Activate EMPTY-CODE (GET)
*/
Route::get('/account/activate/', array(
'as' => 'account-activate',
'uses' => 'AccountController#getActivateEmpty'
));
They are meant to pick up the code from a URL like this: http://localhost:81/account/activate?duYCzo5TEhmRFMBnDEJUSY4EO81EBCJlOyccVBNxpNPksBg6bJJrvUVV4XnX
Unfortunately as you can see the URL isn't activate/code it's activate?code.
This is the code creating the URL (its in a Mail function):
'link' => URL::route('account-recover-code', $code)
What can I change to make sure my route works as intended?
You may try this:
URL::route('account-activate', array('code' => $code));
Also use only one route declaration and make the {code} optional using ? like this:
Route::get('/account/activate/{code?}', array(
'as' => 'account-activate',
'uses' => 'AccountController#getActivate'
));
The URL::route() method expects the route name, which is as value in the route declaration and in {code?} the ? made the parameter optional so if you pass a code to your route then you can pass it as array('code' => $code) and if you dont want to pass the parameter then just use following code to generate the URL:
URL::route('account-activate');
In this case your method should be like:
public function getActivate($code = NULL)
{
if(!is_null($code)) {
// $code is available
}
else {
// $code is not passed
}
}

Resources