How to work with subdirectory controllers in CodeIgniter 4? - codeigniter

I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.

As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.

php spark make:controller /Subfolder/ControllerName

$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.

The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});

Related

Laravel: Grab data from the Controller from inside a view composer

Atm I'm creating this view composer for fun. It is collecting .js filenames and then passing it to the layout to be linked. The filenames used depend on the current page. For example a lower ranked page like Slides, doesn't include ajax requests used in UserManagement. Please don't ask me why I would do this xD. Im planning to validate requests anyway. Just being bored.
Anyways, as I'm quite new to laravel I'm still looking for more efficient ways to do things.
Atm Im accessing the file names staticly. The Controller now looks like this
class Controller extends BaseController
{
public static $js_file_names = [];
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
In the pagecontroller I construct the filenames:
class SlidesController extends Controller
{
public function __construct()
{
parent::$js_file_names = ['ccs', 'ajax-updates'];
}
And finaly I retreive them inside the registered Viewcomposer like this:
(during development $use_filenames has all files as default)
public function __construct()
{
$filenames = Controller::$js_file_names;
if( !empty($filenames) )
$this->use_filenames = $filenames;
var_dump($this->use_filenames);die;
}
It all seems to be working fine, but the big question is, is there a better way to access controller data from inside a viewcomposer? Every time I try to google this, I get results like 'passing data to views' etc, which is not rly the problem.
Update:
Another idea I had is to store all the filenames to be used in an array inside the viewcomposer itself, and check if the current page exists in that array. This would keep the controllers cleaner.
Using a view composer doesn't really make sense in this situation. Since your controllers already 'know' which files they intent to share, you may as well just pass them to the view like so:
class SlidesController extends Controller
{
public function __construct()
{
View::share('user_filenames', ['ccs', 'ajax-updates']);
}
}
A composer is more for sharing concrete elements such as collections of users, a service provider or some other class instance, for example.

Codeigniter Changing routes makes $this->uri->segment() not working

I've altered routes.php
$route['category']='Home/category';
for making url look like www.website.com/category insteead of www.website.com/Home/category. Since Home is my default controller.
but if i am using $this->uri->segment(); inside category function, its not working. this is my controller
class Home extends CI_Controller {
public function category()
{
$value=$this->uri->segmet(3);
}
}
And my url is www.website.com/category/books
I am getting result if I dont alter routes. But by altering routes, I need this to work. Please help me. Thanks
You can debug what you have there by:
var_dump($this->uri->segment_array());
this will give you array of all segments in URI.
Also you can try to debug with this method:
var_dump($this->uri->rsegment_array());
this will give you array of all routed segments in URI
Respectivelly, you can use $this->uri->segment() or $this->uri->rsegment() what ever you find more appropriate for your application.
hello please check segment spelling in your code
class Home extends CI_Controller {
public function category()
{
$value=$this->uri->segmet(3); //wrong
$value=$this->uri->segment(3);
}
}
First of all you need to load URL library and than if your URL is:
www.website.com/category/books
And you want to get books from URL than segment should be:
$value=$this->uri->segment(2); //books
My URL libraries are in autoload. Anyway I solved it. I just configured my routes like this
$route['category/(:any)']='Home/category/$1'

How can I add an external code or script to Codeigniter as class library?

I have an external script or code, for example, including functions, classes, and database inside it. Is there any way to add it to Codeigniter as class library? If yes, please explain more details as possible. My script has some important files included a number of good functions and classes, even database connection and mysql queries inside, but I am confused about how to add it to Codeigniter without rewriting code lines in Codeigniter. Suggest me some good methods to resolve my problem effectively as possible because I do not want to spend time on rewriting and I am a new beginner of Codeigniter. Thank you very much for your help!
The user guide is pretty well explained
http://www.codeigniter.com/user_guide/general/libraries.html
http://www.codeigniter.com/user_guide/general/creating_libraries.html
go to application/library put your class into
Library file name lib.php
class lib {
public function __construct()
{
}
public function test(){
return "welcome";
}
}
go to application->controllers
class Hall_list extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library("lib");
echo $this->lib->test();
}
}
In this way you can load library to your controller

how to debug data query in laravel

This is a beginner question, but I searched for an hour and couldn't find an answer.
I am trying to write a simple data query which I included in my HomeController
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
}
$programs=DB::table('node')->where('type', 'Programs')->get();
$programs is undefined so I am guessing that my query didn't work but I have no idea how to debug it. I tried installing firebug and phpbug and chromphp tools but they don't seem to show anything. My apache log doesn't show anything either. Am I missing something? How do I debug this?
You can't use an expression outside of a method when using a class, instead you need to put it inside a method like:
class HomeController extends BaseController {
public function getPrograms()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
// pass the $programs to the programs view for showing it
return View::make('programs')->with('programs', $programs);
}
}
So, for example, if you have a route like this:
Route::get('/programs', 'HomeController#getPrograms');
Then you may use an URL like: example.com/programs to invoke the getPrograms method in the class HomeController.
Probably this answer doesn't help much but I think you should learn the basics (PHP Manual) first so read books and articles online and check the Laravel website to read the documentation.
You can pass the result of that query to the view like so:
class HomeController extends BaseController {
public function showWelcome()
{
$programs = DB::table('node')->where('type', 'Programs')->get();
return View::make('hello', array('programs' => $programs));
}
}
And in your view you will have access to the $programs variable.
I don't know If i'm missing the point, but can't you use the "dd($programs)" to check what is or not is inside the variable?

Zend MVC load config based on parameter in Url. Where is the right point dispatch/router/controller?

I'm defining a partner through a route based on the url e.g.
my.domain.com/:partner/:controller/:action
Now I want load the config file, databases for the partner before the front controller is called.
Where do I locate this code?
How do I get/set the variables/db, that they are later available in the controller?
I know I could do this through a controller helper but I guess this is not the best point to do it?
Yes, a controller plugin is the way I'd do it:
class MyPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
switch($request->getParam('partner')) {
//... do something based on the possibility
}
}
}

Resources