send get request to specific file - laravel

I have a file called: test.php located in public/inc/test.php. Through jQuery, I can make a get request through jQuery ($.get), by typing:URL: "public/inc/test.php?" + param1` etc.
I want to do the same, but through Laravel. I want to have a route called: /sendparams/{param1}/{param2}, and do a get request to the test.php file from inside a route in web.php and pass the params variables.
How should the route be written?
edit:
by implementing the classes in laravel as external classes, this is no more needed.

you have in your laravel project a file named web.php inside the routes folder.
Define within the web.php file the below code:
Route::get('/sendparams/{param1}/{param2}', 'ControllerName#index');
Perform the below command to create the controller
php artisan make:controller ControllerName
Open ControllerName (app/Http/Controllers/ControllerName)
Your new controller has an structure like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MailController extends Controller
{
public function __construct()
{
}
/* This is the method executed in your route defined in web.php*/
public function index(Request $request) {
$param1 = $request->param1;
$param2 = $request->param2;
$allParams = $request->all(); // returns an associative array with all params
}
}
you can check your routes performed the below command
php artisan route:list
Regards

Related

category controller not found laravel with resource route

I have a resource route for category controller
// category controller routes
Route::resource('api/category', CategoryController::class);
categorycontrooller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Category;
class CategoryController extends Controller
{
public function pruebas()
{
return "controlador de categoria";
}
public function index()
{
$categories = Category::all();
return response()->json([
'code' => 200,
'status' =>'success',
'categories' => $categories
]);
}
}
php artisan route:list
GET|HEAD api/category ..... category.index › CategoryController#index
testing it with postman I got a 404 not found
BadMethodCallException: Method App\Http\Controllers\CategoryController::show does not exist. in file /var/www/html/api-rest-laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 68
I dont know what is hapenning.
You are getting the error because the Resource Controller seems to be incomplete.
You could either:
Create the resource controller. (https://laravel.com/docs/9.x/controllers#resource-controllers)
Route resource only for index. (https://laravel.com/docs/9.x/controllers#restful-partial-resource-routes)
To follow step 1:
Delete/Rename the existing file and run:
php artisan make:controller CategoryController --resource
To follow step 2:
Edit your route to look like:
Route::resource('api/category', CategoryController::class)->only([
'index'
]);
It's probably because you are trying to access the single category and that route uses show method in the controller which is not defined. Also it's better to use plural name for your resource routes:
Route::resource('api/categories', CategoryController::class);

Target class [App\Http\Controllers\LoginController] does not exist

Laravel 6.x.
I'm creating custom multi-authentication Panels(Staff,Student,Admin) from Single Login Page.Error already mentioned in the title. also without using php artisan ui bootstrap --auth.
Web.php file.
Route::get('/index/sign-in', function () {
return view('log-in');
});
Route::get('/index/admin', function () {
return view('admin-dashboard');
});
Route::get('/index/student', function () {
return view('student-dashboard');
});
Route::get('/index/staff', function () {
return view('faculty-dashboard');
});
Route::middleware('auth')->group(function () {
Route::post('/index/dashboard/','LoginController#postlogin')->name('postlogin');
Route::post('/index/logout','LoginController#postlogout')->name('postlogout');
});
LoginController.php file
public function postlogout()
{
auth()->logout();
//session()->flash('message', 'Some goodbye message');
return redirect('/index/sign-in/');
}
public function postlogin()
{
$role=(Auth::user())->user_role;
if ($role=='admin'){
return 'index/admin';
}
elseif ($role=='staff'){
return 'index/staff';
}
elseif ($role=='student'){
return 'index/student';
}else
return 'index/sign-in';
}
}
If you didn't move the controller from his default location, is inside the Auth folder, in the controller folder (app/Http/Controllers/Auth)
So as your error Target class [App\Http\Controllers\LoginController] does not exist it's searching for the controller in the Controllers folders but not in the Auth subfolder, so The route is wrong.
Route::post('/index/logout','LoginController#postlogout')->name('postlogout');
should be
Route::post('/index/logout','Auth\LoginController#postlogout')->name('postlogout');
Best Regards.
I'm not sure if this is the default in Laravel 6, but the LoginController is likely under the Auth folder/namespace`:
app
- Http
-- Controllers
--- Auth
---- LoginController.php
...
In this case, you need to reference the namespace in your routes:
Route::middleware('auth')->group(function () {
Route::post('/index/dashboard/', 'Auth\LoginController#postlogin')->name('postlogin');
Route::post('/index/logout', 'Auth\LoginController#postlogout')->name('postlogout');
});
In your web.php file check all namespace passed, if their directory root is passed correctly or not.
in the upper part of web.php file you need to make sure of each controller root.
Such as you are using LoginController so you must pass use App\Http\Controllers\LoginController; or use App\Http\Controllers\Controller\LoginController;
according to your Http\Controller project structure
If you did not create the controller with the artisan command, delete it and create it with the php artisan create:controller LoginController command. This should get the problem resolved.
Can you check the namespace of the controller
namespace App\Http\Controllers\Auth;
class LoginController extends Controller
All the above solution didn't work for me, what did was using
php artisan route:clear

Laravel Controller not found

I'm learning laravel and I've tried to create a Controller.
I'm really new, so, please, elaborate.
I've used the following command to create the Controller
php artisan make:controller Api/EstadoController
So, EstadoController is under Controllers/Api
I also created a route at api.php
Route::namespace('API')->name('api.')->group(function() {
Route::get('/estados', 'EstadoController#index')->name('estados');
});
EstadoController has index function and correct namespace:
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class EstadoController extends Controller
{
public function index(){
return Estado::all();
}
}
Here is the error page:
Changing
API
to
Api
in my route resolved, like route creation.
Route:
Route::namespace('Api')->name('api.')->group(function() {
Route::get('/estados', 'EstadoController#index')->name('estados');
});
Please try to make route like this:
Route::get('/estados', 'Api\EstadoController#index');

Error on finding controller from Laravel API Router

I created a fresh Laravel framework.
I created a controller named PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Http\Controllers\Controller;
class PostsController extends Controller
{
public function index()
{
$posts = Post::get();
return response()->success(compact('posts'));
}
}
Then I created a route in the file api.php:
Route::get('posts', 'PostsController#index');
I ran the command
$ php artisan serve`
and I tested the URL
localhost:8000/api/posts
This error occurs:
BadMethodCallException
Method Illuminate\Routing\ResponseFactory::success does not exist.
file: vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php
line: 100
throw new BadMethodCallException("Method {$class}::{$method} does not exist.");
I can't understand why this happened. Please help me.
There is no success method on the ResponseFactory. You can find the available methods here.
You are calling a macro function that is not registerd to the responseFactory.To use success method,Create your custom responseServiceProvider and write this inside boot()
Response::macro('success',function($data){
return Response::json([
'data'=>$data,
]) ;
});
And then register your ResponseServiceProvider to app.php by adding your Class name to the array called providers. This is how you add to the array
App\Providers\ResponseMacroServiceProvider::class

How to show page via resource controller laravel 5?

Home.blade.php is starting when I route it directly but not when I call it from resource controller !!
Route::resource('list','listcontroller');
And I call it in index method :
class listcontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return View('list.Home');
}
Change your index function to the following-
public function index()
{
return View('Home');
}
And you will get the corresponding view at base_url/list
or if you want to get with /list/home url then change your route to-
Route::resource('list/home','listcontroller');
If you want to set a base_url othen you can use route prefix
Route::group(['prefix' => 'list'], function () {
Route::resource('home','listcontroller');
});
Any view should be addressed by their path from view folder separated by . (Dot) and with correct character case.
For example if you have home.blade.php in view folder you should call it by view(“home”).
Beside that you can address to any routes by “route” helper function and passing route name to it.
As #amini.swallow said you can reach to your route list by running php artisan route:list command.
In your case you can create link like this :
<a href=“{{route(“list.index”)}}”>click here</a>
Hope it helps.
1)Your controller name is not standard write listController.
2)make sure about your route names:
php artisan route:list
3)in blade write
and try it

Resources