0

I am trying to create a view with four inputs which all enter that data into an sql database (using Laragon).

When I try click on the submit button I get an error saying "Target class [BikeController] does not exist."

Here is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class BikeController extends Controller
{
    function add(Request $req)
    {
print_r($req->input());
    }
}

Here is my routes

<?php

use Illuminate\Support\Facades\Route;
use \app\Http\Controllers\BikeController;


Route::view('/add', 'addView');


Route::post('/add', 'BikeController@add');


Route::get('/', function (){

    return view('bikesView');

});

Route::get('/delete', function (){

    return view('deleteView');

});

Route::get('/edit', function (){

    return view('editView');

});

Link to a pic of my file structure

Currently I'm just trying to print out the data input on the screen but will eventually link it to mysql.

3 Answers3

1
=> Open App\Providers\RouteServiceProvider.php and uncomment this line

protected $namespace = 'App\\Http\\Controllers';
Switi
  • 359
  • 3
  • 6
  • 3
    Please edit your answer to contain more details, such that others can learn from it – Nico Haase May 02 '21 at 16:50
  • If you create a new Laravel 8 project,there is no namespace prefix being applied to your route groups that your routes are loaded into. This causes en error that says Target class does not exist. – Switi May 02 '21 at 16:59
  • 1
    Please **edit your answer** to contain all relevant information – Nico Haase May 02 '21 at 16:59
  • Though there is a mention of a $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it by itself won't add these namespace prefixes, you would still have to make sure you would be using this variable when adding the namespace to the route groups. – Switi May 02 '21 at 17:07
  • With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions. Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example. – Switi May 02 '21 at 17:08
  • // protected $namespace = 'App\\Http\\Controllers'; that is commented on your RouteServiceProvider.php file that's why error will generate Target class [Controller] does not exist and also not run your route so you can uncommented that line solved that problem. i can also face that error i hope help for you. – Switi May 02 '21 at 17:14
  • 1
    Please add **all** clarification **to your answer by editing it**. Don't use the comment section for such important explanation – Nico Haase May 03 '21 at 05:21
0

in your controller change this:

namespace App\Http\Controllers\BikeController;

to:

namespace App\Http\Controllers;

in namespace you just mention the path to the file

0

First check your controller if there is any error solve it, then check your web.php, if there is not an error and after that run those artisan commands for clearing the cache.

Controller:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BikeController extends Controller {
    function add(Request $req) {
        print_r($req->input());
    }
}

web.php

<?php
use Illuminate\Support\Facades\Route;

Route::view('/add', 'addView');
Route::post('/add', 'BikeController@add');
?>

Run the below artisan command:

//---Remove Routes Cache
php artisan route:clear;
//---Flush the application cache
php artisan cache:clear;

And if you want to regenerates the list of all your project classes run this composer command:

composer dump-autoload;
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38