I am trying to add controller view in my existing project that consider model view controller structure in php with laravel.
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata::create');
}
}
When I implement this, it shows me error for,
InvalidArgumentException
No hint path defined for [CashFlowdata].
I have added file in route.php and web.php as added other controller data. Only for this one it shows message like this.
you code is wrong you should to something like this
class CashFlowdataController extends Controller {
public function index() {
return view('CashFlowdata.create');
}
}
here CashFlowdata.create
its means in laravel
folder structure should be
view>CashFlowdata>create.blade.php
laravel view() function is a helper to load view file
ref link https://laravel.com/docs/8.x/helpers#method-view
I had the same issue in nwidart/laravel-modules due to module.json file was miss place.
I move the file to the root of module now working fine.
Related
My Master controller located in "admin" folder. View image
here
namespace App\Controllers\admin;
use CodeIgniter\Controller;
class Master extends Controller
{
function __construct(){
helper('url');
}
public function index()
{
$data["content"]="view_home";
echo view('template/template', $data);
}
}
In my Routes.php i added this
$routes->get('admin/master','Master::index',['namespace','App\Controllers\admin']);
when i access the page in the browser i get this error
404 - File Not Found
Controller or its method is not found: {0}::{1}
What am i missing?
My silly mistake when setting up the route. I've put a "," instead of "=>". See the correct route below.
$routes->get('admin/master','Master::index',['namespace' => 'App\Controllers\admin']);
I'm facing one weird problem with my Codeigniter project.
I have two model files in the following paths:
models/public/Dish_model.php
models/admin/Dish_model.php
For front-end and back-end models respectively.
In the controller which is in the following path:
controllers/admin/Dish.php
I'm trying to load the admin area model file using:
$this->load->model('admin/dish_model');
But it is loading the public model file.
Even if I comment this line out the public model file still gets loaded.
This all happened suddenly it was working fine before and I haven't changed any of the mentioned files recently.
Any help?
In case anyone else encounter the same issue.
In my case and after carefully following the execution path of the controller I found that the other model was being loaded by a library in the autoload list.
Removing that library from the autoload array fixed the problem.
to call/load a model from its subdirectory or subfolder.
models/public/Dish_model.php
let's say in Dish_model
class Dish_model extends CI_Model{
public function __construct(){
parent::__construct();
}
public function the_method(){
return 'return value';
}
}
models it's a native ci model directory and public it's a subdirectory. so to load the Dish_model do this on controller
$this->load->model('public/Dish_model','DModel');
the DModel it is like an alias.
so to call the model from the controller is
echo $this->DModel->the_method();
and it will return return value
hope its help.
I am familiar with Laravel 4 routes, but I am experiencing some problem with Laravel 5.
I code route.php as:
Route::get('/','HomeController#index');
and my HomeController.php is the following:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('index');
}
}
The output page displays as:
Whoops,looks like something went wrong.
The Route annotation file seems to be perfect.
The same case occurs for folder routing too!!
Please help me out.
First of all you should follow the instructions from James Njuguna in a comment to your question. Withoug debugging we can only guess whats going wrong.
In your case, most likely your error is, that the line
return View::make('index');
is causing an exception, because class App\Http\Controllers\View is not found. In this file a namespace is used, so you have to reference the root namespace like:
return \View::make('index');
OR you use a helper function
return view('index');
This function is documentated at http://laravel.com/docs/5.0/helpers#miscellaneous
If that's still failing... maybe you don't have an index.php or index.blade.php in your resources/views folder.
All, what #shock_gone_wild and #JamesNjuguna said is true. The reason of an error occurring is that you do not use namespaces when you call View.
For testing you can simply return text from a controller like this:
public function index()
{
return 'test'
}
and when it returns a result you can see what was the reason for the error and than you can change it with view global function, like #JamesNjuguna said.
Try this
public function index()
{
return view('home');
}
In laravel 5 the view class is not illuminated using a capital letter at the beginning
The error I'm getting is that the controller doesn't exist even though I know it does, here's the code.
Route.php
Route::get('mdpay/template', array("uses" => "templateController#index"));
templateController.blade.php
class templateController extends BaseController {
public function index()
{
echo "made it";
}
}
Why might I be getting this error: Class TemplateController does not exist
================= UPDATE: ==================
Ok, so I've created the correct route, renamed my file, and corrected the class name and I'm still coming up with that error.
File Names:
templateController.php
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// app/views/myView.blade.php
echo "hello";
}
}
My route is:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Still receiving Controller Doesn't Exist error. All my other controllers (3 others) are working except this one.
If you are using the standard composer classmap autoloader you need to composer dumpautoload everytime you create a new file.
So to create a new controller with the standard composer setup given by Laravel:
Create a new file in app/controllers named TemplateController.php
Open up terminal and run composer dumpautoload
As previous users have said, only view files should end with .blade.php.
If you're using Laravel 8, add this line to your RouteServiceProvider.php (you can search it by using CTRL + P):
protected $namespace = 'App\Http\Controllers';
This solved the issue for me.
It should be:
// File Name: TemplateController.php
class TemplateController extends BaseController {
public function index()
{
// return "made it"; // or
// app/views/myView.blade.php
return View::make('myView');
}
}
Route for that:
Route::get('mdpay/template', array("uses" => "TemplateController#index"));
Use blade in a Blade view, i.e: myView.blade.php basically stored in app/views/ folder. Read more about blate template on Laravel website.
Controllers live in the app/controllers directory and should remain there unless you have your own namespaced structure.
The reason you're getting a Class TemplateController does not exist is because it doesn't, firstly, your class is called templateController and secondly, it exists as templateController.blade.php which wouldn't be loaded in this way.
Blade files are for views, and only views within app/views or a custom views directory should end with .blade.php.
Create the file app/controllers/TemplateController.php and add the following code to it.
class TemplateController extends BaseController {
public function index()
{
echo "made it";
}
}
Now on the command line, run the command composer dumpautoload and change you route declaration to:
Route::get('mdpay/template', array('uses' => 'TemplateController#index"));
Now it should all work.
In case you're using Laravel 9 and the error is like Illuminate\Contracts\Container\BindingResolutionException and Target class <controller name> does not exist. when trying php artisan route:list on terminal.
This is the setup that I do:
Add protected $namespace = 'App\\Http\\Controllers'; to RouteServiceProvider.php
Add 'namespace App\Http\Controllers;' to the controller file.
Do php artisan optimize on terminal
(Just to make sure the route already there) Do php artisan route:list again on terminal, and the controller route should be displayed.
I'm a new user of both Stack Overflow and laravel.
I just started to learn L4 in their official site but now I'm having a small problem in loading the views.
Now my views is not working properly. Any file I call under the views dir results in "file doesn't exist" error.
This is my code ....
Authors controller is:
class AuthorController extends BaseController{
public $restful = true;
public function get_index()
{
return View::make('authors.index');
}
}
On routes.php I added
Route::get('authors',array('uses'=>'authors#index'));
under views/authors/index.php
Any html text
Change
Route::get('authors',array('uses'=>'authors#index'));
to
Route::get('authors',array('uses'=>'AuthorController#get_index'));
If you dont plan on doing complex logic you could skip the controller and just call the view from the router. And also its a good practice to keep the functions as camelCase so in your case getIndex().
Route::get('authors', 'AuthorController#getIndex');