Codeigniter v4:Custom View Does Not Return - php

I'm using Codeigniter v4 and wanted to show a custom view to users, so I made a Controller named Test:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return view('custom');
}
}
And a Model named Usermodel:
<?php
namespace App\Models;
class Usermodel extends CI_Model
{
public function getusers()
{
return [
['firstmame'=>'Mohd','lastname'=>'Saif'],
['firstname'=>'Syed','lastname'=>'Mujahid'],
['firstname'=>'Mohd','lastname'=>'Armaan']
];
}
}
And the view custom.php already exists in the Views folder.
But when I load the url http://localhost/ci4/public/index.php/test I get 404 Not Found error message.
Also I tried http://localhost/ci4/public/index.php/test/index but shows the same message.
So how to load this method from the custom controller class in Codeigniter v4 properly?

Except you're not parsing the $data to the view (you should do that adding a second parameter to view('custom', $data)), the code doesn't seem to be the problem: When a view could not be loaded in CI, it shows a specific error message (CodeIgniter\View\Exceptions\ViewException), not a 404 Not Found message.
Probably the problem is in another part of your project.

Try this
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load>model('Usermodel');
$data['users'] = $this->Usermodel->getusers();
return $this->load->view('custom',$data);
}
}
add the $data array to your views

Related

Call Model from custom component into Joomla 4 controller

I have been working in Joomla 4.2.5 and I need to convert my custom components from Joomla 3 to Joomla 4 compatible. I am trying to call model from my custom component into Joomla user controller file, but I am getting an error and code does not perform execution.
I have tried few code but it is not working.
Joomla 3 code for call model from custom component into user controller:
UserController.php
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_mycomponent/models', 'MycomponentModel');
$model = JModelLegacy::getInstance('Mycomponent', 'MycomponentModel');
Here is my model file code in
components/Mycomponent/Models/mymodel.php
class MycomponentModelmycomponent extends JModelLegacy
{
public function somefunction(){
}
}
Below is my attempt to convert above code into Joomla 4:
UserController.php
Use Joomla\CMS\MVC\Model\BaseDatabaseModel;
class UserController extends BaseController
{
public function login()
{
login functions....
BaseDatabaseModel::addIncludePath(JPATH_SITE .'/components/com_mycomponent/models');
$model = BaseDatabaseModel::getInstance('Mycomponent', 'MycomponentModel');
$model->myfunction($argument);
}
}
components/Mycomponent/Models/mymodel.php
namespace Joomla\Component\mycomponent\Site\Model;
use Joomla\Registry\Registry;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
class IbousersModelIbousers extends ListModel
{
public function myfunction($argument){
}
}
But it is not working, can anyone suggest me what is wrong or how can I call model from another component in Joomla 4?
In Joomla4, you get the model from the MVCFactory.
So the equivalent of:
BaseDatabaseModel::addIncludePath(JPATH_SITE .'/components/com_mycomponent/models');
$model = BaseDatabaseModel::getInstance('Mycomponent', 'MycomponentModel');
Would be:
$mvc = Factory::getApplication()->bootComponent('com_mycomponent')->getMVCFactory();
$model = $mvc->createModel('MycomponentModel', 'Site');

How to include a function of a controller inside another in Laravel4 when name space is changed

I have two controller
apiController
2.LeadController
now my lead controller looks like this
namespace leads;
class LeadsController extends \BaseController {
public function AddLeadsHistory(){
$data['leads_id'] = Input::get('leads_id');
$data['comment'] = Input::get('comment');
$data['date_added'] = DB::raw('Now()');
App::make('apiController')->addCallback( $data['leads_id'],$data['date_added']);
}
}
I am getting following error
Class 'leads\App' not found
Can anyone let me know how I can use the controller in this scenario

Single page controller not working

So I have created a single page as subpage at:
/application/single_pages/leden/mijnaccount.php
Added it at the single pages list in dashboard.
The page is working fine.
But when I add a controller at:
/application/controllers/single_page/leden/mijnaccount.php
With the following contents to test:
<?php
namespace Application\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
class Mijnaccount extends PageController
{
public function on_start()
{
exit('Started');
}
public function view()
{
exit('View');
}
public function on_before_render()
{
exit('Before render');
}
}
None of those exit() functions get called. What am I doing wrong?
The solution seems to be to add the subfolder to the namespace:
namespace Application\Controller\SinglePage;
Becomes:
namespace Application\Controller\SinglePage\Leden;

How to call a model function inside the controller in Laravel 5

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

Call to undefined method Illuminate\Database\Query\Builder::groups() eager loading

I am creating a website using laravel. I have a small issue with eager loading. I have already made several websites with laravel, but still I can't find what is wrong here.
This is my config model:
<?php
class Config extends \Eloquent {
protected $table = "configs";
public function groups() {
return $this->hasMany('ConfigOptionGroup', 'config_id');
}
}
And this is my testcontroller class:
<?php
namespace WebsiteController;
class DemoController extends \BaseController {
public function getTest() {
$c = \Config::where('id', 1)->with(['groups' => function($q){
$q->whereNull('config_option_group_id');
}])->first();
return $c;
}
}
Whenever I surf to the url that calls the getTest method I get an error saying Call to undefined method Illuminate\Database\Query\Builder::groups(). However, the function groups() exists in the Config model.
When I remove the with() function from the query, it works just fine. But I can never load the groups via the relation.
Is there anyone who can help me with this problem?
Update: I have removed the Config facade by commenting out the line in /app/config/app.php.

Categories