i`m using codeigniter 4 and try to use a controller from other controller
i have 2 controllers in Controllers folder, Home.php dan Shop.php
Home.php
<?php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
return view('welcome_message');
}
function validation()
{
$shop = new Shop();
echo $shop->product('laptop', 'brand');
}
}
and Shop.php
<?php
namespace App\Controllers;
class Shop extends BaseController
{
public function index()
{
return view('shop');
}
public function product($type, $product_id)
{
echo "this is a $type and $product_id";
}
}
when i try to load localhost:8080/home/validation
the result still :
Whoops!
We seem to have hit a snag. Please try again later...
how to do the right way ?
thank you
it is solved. i change my installation with composer.
it is runs good now.
before, i only extract the ci4 source, install intl extension and create .env
Related
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
My goal is to include codes from another sources which is located in resources/views. I have tried using resource_path('views/myfiles.php') but it does nothing.
Controller
class MyController extends Controller
{
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$theFilesLocation = "resources.views" . $request->input('name');
#include($theFilesLocation) //something like this
}
}
}
myfiles.php
<?php
dump("if this shows up, then the code works")
?>
Try bellow code but I think it is not a good way.
class MyController extends Controller
{
require_one(resource_path('views/myfile');
}
Or with Laravel File facade
class MyController extends Controller
{
\File::requireOnce(resource_path('views/myfile');
}
You should create a class and put your code there then call it from the controller is a better solution.
What you are looking for is a trait. This allows the easy sharing of code and functionality without having to inherit from a specific base class causing an inheritance hell.
namespace MyCode\Traits;
trait SharedCodeForThing {
public function blaTheBla() {
dump("if this shows up, then the code works");
}
}
and then in your controller
use MyCode\Traits\SharedCodeForThing ;
class MyController extends Controller
{
use SharedCodeForThing;
}
Now if you wish to just render the contents of the view which it seems you're after:
public function test(Request $request)
{
if($request->input('name') == "chair")
{
$view = view('resources.views' . $request->input('name'));
return $view->render();//or echo $view->render(); whatever you like
}
}
I am fresher in cakephp. For my current project, I am using CakePHP skeleton app. Everything going fine. But when I am creating new controller for admin panel then it showing this message Did you really think you are allowed to see that?. Someone please help me.
I am showing my codes below:
Route:
Router::prefix('admin', function ($routes) {
// Other routes are here.
$routes->connect('/sections', ['controller' => 'Sections', 'action' =>'index']);
}
SectionsController.php
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class SectionsController extends AppController {
public function index() {
echo "I am for sections page";
}
}
This controller is locate in src\Controller\Admin folder
Below is my error message.
probably this would be the solution.
use Cake\Event\Event;
class YourController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow('index');
}
}
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;
When autoloading classes, the following runs without a problem:
<?php
namespace App\Resources;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
How does this work? I never imported the Controller class:
<?php
namespace App\Resources;
use App\Resources\Controller;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
If you use non-qualified class name (without the namespace), PHP assumes you mean the current namespace. The code above works because both Home and Controller are in the same namespace App\Resources.