Laravel 5.1
This seems strange to me:
Route::group([
'middleware'=>['auth','acl:view activity dashboard'],
'prefix' => 'api/v1'
], function(){
Route::controller('investment-transactions', 'Api\V1\Investments\InvestmentTransactionsController');
Route::controller('investment-transactions/{offeringID}', 'Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering');
});
Seems pretty normal to me, the controller:
namespace App\Http\Controllers\Api\V1\Investments;
use App\Brewster\Models\Company;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvestmentTransactionsController extends Controller {
public function __construct() {
}
public function getIndex() {
echo 'Here';
}
public function getTransactionsForOffering($offeringID) {
echo $offeringID;
}
}
Ok so the action and the controller do exit, but when I run: php artisan routes:list I get:
[ReflectionException]
Class App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering does not exist
Well obviously App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering is not a class, how ever: App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController is and getTransactionsForOffering is an action.
Whats going on?
I believe your problem is in the routes.php we can use controllers as follows
Route::get('investment-transactions', 'InvestmentTransactionsController#index');
Route::get('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
By default, our controllers are stored in App/http/controllers folder and laravel know it.
I believe you only need to reference the Class like so:
Route::controller('investment-transactions','InvestmentTransactionsController#Index'); //make sure you create a function for the index
Route::controller('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
Assuming you need to show a view for the route investment-transactions create the following function in your controller:
public function index()
{
return view('name-of-your-view-file');
}
Related
I have created the web route correctly.
use App\Http\Controllers\FrontendCourseController;
Route::get('update-item', [FrontendCourseController::class, 'update'])->name('update-item');
and add a method update. Also, I have created controller correctly.
<?php
namespace App\Http\Controllers;
use App\Helpers\CurrencyHelper;
use Auth;
use DB;
use App\Models\Course\Course;
use App\Models\Item;
class FrontendCourseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function update()
{
dd('Page Update is working');
return view('update-item');
}
}
But I keep getting this error message
Method App\Http\Controllers\FrontendCourseController::update does not
exist.
Despite clearing caches using
php artisan route:cache
Where am I doing wrong?
**Target class ** does not exist. ?? Why I didn't understand
Error Is lluminate\Contracts\Container\BindingResolutionException
Target class [app\Http\Controllers\FrontEnd\IndexController] does not exist.
Illuminate\Container\Container::build
C:\xampp\htdocs\check-time.com\vendor\laravel\framework\src\Illuminate\Container\Container.php:875
I am Using Laravel 8. Environment information
Laravel version
8.47.0
Laravel locale
en
Laravel config cached
true
PHP version
8.0.6
<?php
namespace App\Http\Controllers\FrontEnd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class IndexController extends Controller
{
public function UserGuide(){
return view('FrontEnd.FrontWeb.User-Guide');
}
public function About(){
return view('FrontEnd.FrontWeb.about');
}
public function Download(){
return view('FrontEnd.FrontWeb.download');
}
public function ContectUs(){
return view('FrontEnd.FrontWeb.contact-us');
}
}
Here is My Web.php Route
<?php
use Illuminate\Support\Facades\Route;
use app\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Backend\AdminDashboardController;
use App\Http\Controllers\Backend\CategoryController;
use App\Http\Controllers\UserGuide\UserGuideController;
use App\Http\Controllers\Backend\AdminController;
use App\Http\Controllers\FrontEnd\IndexController;
// front end Route All GO Here
Route::get('/', function () {
return view('FrontEnd.FrontWeb.index');
});
Route::get('/User-Guide',[IndexController::class,'UserGuide'])->name('User.Guide');
Route::get('/about',[IndexController::class,'About'])->name('About.Page');
Route::get('/check-time-Software-download',[IndexController::class,'Download'])->name('Download.Page');
Route::get('/contact-us',[IndexController::class,'ContectUs'])->name('Contact.Us');
// Admin Route All Here
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('logout/',[AdminController::class,'logout'])->name('user.logout');
Namespaces are case-sensitive. In Laravel, the app namespace is with a lowercase a.
Method 1 :
Just import your controller in the routes file, like the following example, or use full path with controller file.
use App\Http\Controllers\IndexController;
Route::post('/about' , 'IndexController#About');
//OR
Route::get('/about', 'App\Http\Controllers\IndexController#About');
Method 2 :
Go to app/Providers/RouteServiceProvider.php and find and enable this line, as it should be commented.
protected $namespace = 'App\\Http\\Controllers';
I have a simple in routes/web.php file
Route::get(Config::get('constants.ADMIN_PATH') . '/categories', 'AdminControllers\AdminPagesController#index');
I have made a folder AdminControllers and inside that there is a controller named AdminPagesController but i am getting error as
Class App\Http\Controllers\AdminControllers\AdminPagesController does not exist
Whereas i looked into the same folder and class exist. Here is my class code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Change you namespace to
namespace App\Http\Controllers\AdminControllers;
Laravel will resolve controllers based on your name spacing, not on your directory structure.
You should specify the namespace correctly, change it to:
namespace App\Http\Controllers\AdminControllers; // <------- correct this namespace
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Hope this helps!
If you choose to nest your controllers deeper into the **
App\Http\Controllers
** directory, use the specific class name relative to the
App\Http\Controllers
root namespace.
namespace App\Http\Controllers\AdminControllers;
I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function
I am trying to run a sample example using Laravel 4 and i am getting errors like, class not found. Can someone help me out here?
controller
file name : authors.php
<?php
class Authors extends BaseController {
public $restful = true;
public function get_index() {
return View::make('authors.index');
}
}
?>
routes.php
Route::get('authors', array('uses'=>'authors#index'));
Views/authors/index.php
<h1> First program in Laravel 4 </h1>
Fitst of all your authors!=Authors, so make sure of your conyroller name in the Route.
And if you want RESTful controller then you can define your route like Route::controller('baseURI','ControllerName'),
Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method..
To know more check restful-controllers
In your example you have to rename your get_index method to getIndex as L4 is camelCase
//AuthorsController.php
class AuthorsController extends BaseController {
public $restful = true;
public function getIndex() {
return View::make('authors.index');
}
}
//route.php
Route::controller('authors', 'AuthorsController');