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;
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
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
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
}
}
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.
I use subfolder in the Controller 'folder',which works fine..
but when I write the blow code ..php return the error said "Auth is not found ,and the Input'
<?php
namespace website;
use Auth;
use Input;
use View;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('wcsite.index');
}
public function saveHome()
{
$uid = Auth::user()->id;
$websiteData = Input::get('data');
return $uid;
}
}
but when I add 'use Auth,use Input',everything works fine...so ,anyone who can tell me ...is there any way to to this ,which "need not to use Auth,use Input in my subfolder Controllers' Thank you a lot!
and my route is
Route::post('/wcsite',array('uses' => 'website\HomeController#saveHome'))->before('auth');
Your question is a bit confusing. You're saying that the code above is not working because PHP can't find the Auth and Input global class references but your code clearly shows you're importing them correctly.
PHP can't use the global Auth and Input class references without importing them first (which you're doing in the above code). It's going to assume they're located under the website namespace by default.
If you don't want to import hem with use statements you could always reference the global namespace by using a backslash before the class name like the code below:
<?php
namespace website;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return \View::make('wcsite.index');
}
public function saveHome()
{
$uid = \Auth::user()->id;
$websiteData = \Input::get('data');
return $uid;
}
}
That being said, I prefer importing the classes first instead of using backslashes everywhere. It'll provide for much cleaner code.