I cannot bind the "contact" route to the "contact" method which is existing in the "TestController.php" controller
You don't need to include the file extension when you define routes, so your route should be:
Route::get('/contact','TestController#contact');
Then make sure you have a controller with that name in your controller directory:
app/Http/Controllers/TestController.php
namespace App\Http\Controllers
class TestController extends Controller
{
public function contact()
{
// your code
}
}
You can see a working demo here.
I usually find running php artisan route:clear will solve these issues sometimes.
Run Command php artisan optimize will solve issue
Related
I have this code in routes, is it possible to simplify it?
Thanks
Route::get('/post1', function () {
return view("post1");
})->name("/post1");
There is nothing wrong with that code, the only way you can "simplify" that code, better to say "abstract it" is by creating a controller with a method that returns the view.
In your case, if your route is very specific you can create a single action controller with the command:
php artisan make:controller PostController -i.
Then in the controller:
public function __invoke(Request $request)
{
return view("post1");
}
And in your routes file:
Route::post('/post1', PostController::class);
More info in the Single Action Controller docs
and in the views docs
I created ColorController Class using
php artisan make:controller ColorController
The class has been created successfully and exists in the App/HTTP/Controller folder.
I defined route like this
Route::get('color/text', [ColorController::class, 'text']);
And a Method in the Contoller Class like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ColorController extends Controller
{
public function text()
{
return 'Color Controller';
}
}
I visited the like this
http://localhost:8000/color/text
It shows the error message as below
Illuminate\Contracts\Container\BindingResolutionException
Target class [ColorController] does not exist.
http://localhost:8000/color/text
Where did I make the fault? where to correct the process?
I have found the Solution like this...
I had to add this line above in the web.php file
use App\HTTP\Controllers\ColorController;
Now all works fine.
I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.
As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.
php spark make:controller /Subfolder/ControllerName
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.
The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});
My controller is not working when i am attaching router and controller with each other. It is showing the error
Class App\Http\Controllers\HelloController does not exist.
I hope my syntax and everything is right but can't figure out the issue.
Filename is also same as class name.
namespace I used is :
namespace App\Http\Controllers\;
Route code:
Route::get('sayHello', 'HelloController#index');
Controller Code:
class HelloController extends Controller
{
public function index()
{
return view('hello');
}
}
The namespace for the controller should be:
namespace App\Http\Controllers;
Notice it doesn't have "\" at the end.
Once you have changed the above it will be worth running:
composer dumpautoload
When creating a controller in the future it will probably be worth using the artisan command make:controller:
php artisan make:controller SomeNewController
The issue is the <? php at the top of your controller class. It should be <?php (no space)
I am having trouble getting dynamic routes to work in Laravel 4
Routes
Route::any('/browse/{$id}', 'BrowseController#showProfile');
Controller
<?php
class BrowseController extends BaseController {
public function showProfile($id)
{
return $car_id;
}
}
When I go to http://localhost:8000/browse/10018
I receive a not found error
Any Idea what is wrong? Sorry I am new to Laravel
You don't need a $ in the variable name in your route. Try using
Route::any('/browse/{id}', 'BrowseController#showProfile');
Also, you should add validation to only allow numbers:
Route::any('/browse/{id}', 'BrowseController#showProfile')->where('id', '\d+');
The problem is in {$id}, try only {id}