I am having the following error
InvalidArgumentException in FormBuilder.php line 39:
Form class with name App\Http\Controllers\App\Forms\SongForm does not exist.
on Laravel,
SongsController.php class
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use Kris\LaravelFormBuilder\FormBuilder;
class SongsController extends BaseController {
public function create(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
return view('song.create', compact('form'));
}
public function store(FormBuilder $formBuilder)
{
$form = $formBuilder->create(App\Forms\SongForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
// Do saving and other things...
}
}
SongForm.php
<?php
namespace App\Forms;
use Kris\LaravelFormBuilder\Form;
class SongForm extends Form
{
public function buildForm()
{
$this
->add('name', 'text', [
'rules' => 'required|min:5'
])
->add('lyrics', 'textarea', [
'rules' => 'max:5000'
])
->add('publish', 'checkbox');
}
}
routes.php
Route::get('songs/create', [
'uses' => 'SongsController#create',
'as' => 'song.create'
]);
Route::post('songs', [
'uses' => 'SongsController#store',
'as' => 'song.store'
]);
And I do not know where is the problem because the file exist in the project folder.
Explanation of the Error
Here:
$form = $formBuilder->create(App\Forms\SongForm::class, [
'method' => 'POST',
'url' => route('song.store')
]);
You're specifing the class name with a namespace relative to the current namespace:
App\Forms\SongForm::class
the full class name will be built relatively from the current namespace that is:
namespace App\Http\Controllers;
So, the class you're passing as parameter becomes:
App\Http\Controllers\App\Forms\SongForm::class
That class doesn't exists, and so you get the error
How to solve
To solve, you can specify the absolute namespace. Change this:
App\Forms\SongForm::class
to this:
\App\Forms\SongForm::class
and it should work
Related
I have the following test to check my simple crud application in laravel.
I have my SalesManagersTest.php in tests/features
<?php
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SalesManagersTest extends TestCase
{
use RefreshDatabase;
public function test_can_see_index_screen()
{
$response = $this->get('/salesmanagers');
$response->assertStatus(200);
}
public function test_can_see_add_record_screen()
{
$response = $this->get('/salesmanagers/create');
$response->assertStatus(200);
}
public function test_can_add_new_record()
{
$response = $this->POST('/salesmanagers/create', [
'_token' => csrf_token(),
'full_name' => 'Test User',
'email' => 'test#example.com',
'telephone' => '0775678999',
'current_route' => 'Negombo',
'joined_date'=>'2022/09/12',
'comments' => 'Test comment'
]);
$response->assertStatus(200);
$response->assertRedirectedTo('salesmanagers');
}
}
The first two tests work well but the third test is giving me an error
but since I'm trying to insert new record the method has to be POST
this is my web.php
Route::get('/', [ SalesManagersController::class, 'index' ]);
Route::resource('salesmanagers', SalesManagersController::class);
Route::get('salesmanagers.create', [ SalesManagersController::class, 'create' ]);
Route::post('salesmanagers.create', [ SalesManagersController::class, 'store' ]);
What could be the the issue with my test?
The issue is your route definition. It appears you're using a route name for the URI of your route. Change your route to the following and try again:
Route::post('/salesmanagers', [SalesManagersController::class, 'store']);
Oh and POST your data to /salesmanagers not salesmanagers/create.
I look around about this issue but still can't understand why I'm getting error :
Class "App\Models\MenuList" not found
app/Models/Listing.php
<?php
namespace App\Models;
class MenuList
{
public static function all()
{
return [
'id' => 1,
'title' => 'liston one',
];
}
}
routes/web.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\MenuList;
Route::get('/', function () {
return view('listings', [
'heading' => 'lates listing',
'listings' => MenuList::all()
]);
});
Try this
create model using command
php artisan make:model MenuList
App\Models\MenuList.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MenuList extends Model
{
use HasFactory;
public static function listData()
{
return [
'id' => 1,
'title' => 'liston one',
];
}
}
routes/web.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\MenuList;
Route::get('/', function () {
return view('listings', [
'heading' => 'lates listing',
'listings' => MenuList::listData()
]);
});
A few days ago I started learning laravel 7.
I bought a course on udemy.
I got to the part where the real registry system went and started to rewrite the code like in the video, but when I do it I get an error!
Error Message: "Class 'App\Http\Controllers\Validator' not found"
I've been trying to fix this for hours, and I'm not doing well
AccountController.php
<?php
namespace App\Http\Controllers;
class AccountController extends Controller
{
public function getcreate(){
return view('account.create');
}
public function postcreate(){
$validator = Validator::make(Input::all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required|min:6',
'repeat_pass' => 'required|same:password'
));
if($validator->fails()){
die('ERROR');
}
else{
die('Cool');
}
}
}
you need to import validator namespace
use Illuminate\Support\Facades\Validator;
then instead of Input you could use request()->all() helper function
so it will be like this
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Validator;
class AccountController extends Controller
{
public function getcreate(){
return view('account.create');
}
public function postcreate(){
$validator = Validator::make(request()->all(),
array(
'email' => 'required|max:50|email|unique:users',
'username' => 'required|max:20|min:3|unique:users',
'password' => 'required|min:6',
'repeat_pass' => 'required|same:password'
));
if($validator->fails()){
die('ERROR');
}
else{
die('Cool');
}
}
}
You will need to import the Validator class from it's correct namespace which is Illuminate\Support\Facades. So goes with Input class. Best way I can suggest is to add these in aliases section in config\app.php like below:
'aliases' => [
// other imports
'Validator' => Illuminate\Support\Facades\Validator::class,
'Input' => Illuminate\Support\Facades\Input::class,
]
Now, you can simply use them in your controller like below:
<?php
namespace App\Http\Controllers;
use Validator;
use Input;
class AccountController extends Controller
{
// rest of your code
}
You can use Validator namespace in your controller at top like:
use Validator;
I am using Laravel 5.5.40 along with the Zizaco\Entrust Pacakge
In my routes/web.php file i have the following route setup.
Route::group(['prefix' => 'order'], function() {
Route::get('', 'OrderController#getMe');
});
It is supposed to call the getMe() method inside the OrderController.php but it instead redirects to www.mydomain.co.uk/home
namespace App\Http\Controllers;
class OrderController extends Controller
{
public function getMe() {
return "You got me!";
}
}
As a test, I added a __construct function to the OrderController.php to see if the class was even been loaded.
public function __construct() {
dd("Testing");
}
When accessing www.mydomain.co.uk/order i now get
"Testing"
I can't seem to work out why it is not running the getMe() method. Could anyone possibly shine some light on this please?
I have also tried changing the route to use ClientController#list which works fine.
Contents of ClientController.php
namespace App\Http\Controllers;
use App\Client;
class ClientController extends Controller
{
public function __construct() {
//
}
// Display all the clients
public function list() {
$tabContent = [
'display_type' => 'list',
'data' => Client::orderBy('name', 'asc')->get(),
'view_params' => [
'columns' => [
'name' => 'Client Name',
'address_line_1' => 'Address Line 1',
'town' => 'Town',
'county' => 'County',
'post_code' => 'Post Code'
],
'links' => 'client',
'controls' => True
]
];
return view('tables.list', ['data' => $tabContent]);
}
}
It has become apparent that if the controller does not have the constructor function in it, it will automatically redirect to the root of the URI with no error.
public function __construct() {
//
}
So I followed his readme and I have done composer dump-autoload a million times, but still I receive an error.
The code:
'providers' => [
...
Thujohn\Twitter\TwitterServiceProvider::class,
],
'aliases' => [
...
'Twitter' => Thujohn\Twitter\Facades\Twitter::class,
],
In my controller:
class HomeController extends Controller {
public function index() {
$tweets = Twitter::getUserTimeline([
'screen_name' => 'xxxxxxx',
'count' => 10,
'format' => 'json'
]);
dd($tweets);
return view('home');
}
public function about() {
return view('about');
}
}
But I get the error:
FatalErrorException in HomeController.php line 10:
Class 'App\Http\Controllers\Twitter' not found
Um ..... What?
You used non-namespaced name when you refered to Twitter class, so PHP is looking for the class in current namespace. Change that reference to \Twitter or add the following use statement:
use Twitter;