I installed HMVC by wiredesignz but the routes from application/modules/xxx/config/routes.php didn't get recognized at all.
Here is an example:
In application/modules/pages/config/routes.php I have:
$route['pages/admin/(:any)'] = 'admin/$1';
$route['pages/admin'] = 'admin';
If I type the URL, domain.com/admin/pages/create it is not working, the CI 404 Page not found appears.
If I move the routes to application/config/routes.php it works just fine.
How do I make it work without putting all the admin routes in main routes.php?
I searched the web for over 4 hours but found no answers regarding this problem. I already checked if routes.php from modules is loading and is working just fine, but any routes I put inside won't work.
I found a way of making the routes from modules working just fine, I don't know if is the ideal solution but works fine so far:
open your application/config/routes.php and underneath $route['404_override'] = ''; add the following code:
$modules_path = APPPATH.'modules/';
$modules = scandir($modules_path);
foreach($modules as $module)
{
if($module === '.' || $module === '..') continue;
if(is_dir($modules_path) . '/' . $module)
{
$routes_path = $modules_path . $module . '/config/routes.php';
if(file_exists($routes_path))
{
require($routes_path);
}
else
{
continue;
}
}
}
the following solution works fine even if config folder or routes.php is missing from your module folder
Here's the thing: the module's routes.php only gets loaded when that module is "invoked", otherwise CI would have to load all route configurations from all modules in order to process each request (which does not happen).
You'll have to use your main application's routes.php to get this to work. You aren't using the pages segment in your URL, therefore the routing for that module never gets loaded.
I know that's what you wanted to avoid, but unfortunately it's not possible unless you want to get "hacky".
Here's the routing I use to map requests for admin/module to module/admin, maybe you can use it:
// application/config/routes.php
$route['admin'] = "dashboard/admin"; // dashboard is a module
$route['admin/([a-zA-Z_-]+)/(:any)'] = "$1/admin/$2";
$route['admin/([a-zA-Z_-]+)'] = "$1/admin/index";
$route['(:any)/admin'] = "admin/$1";
You just need this https://gist.github.com/Kristories/5227732.
Copy MY_Router.php into application/core/
Related
I'm having some troubles with routing in codeigniter.
My routing file is as below:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
$route['admin/create'] = 'posts/create';
$route['admin/listposts'] = 'posts/list';
$route['admin'] = 'admin/index';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = 'pages/index';
$route['(:any)'] = 'pages/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
All routes work just fine except for the first two:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
When I type mypage/admin/listgallery it calls gallery/list correctly. The problem is when I type the address with the original controller/method (in this case gallery/list) it goes to the list page as well when it should call a 404 error! Every other routing rule I have set does that, except the first two!
Out of the Box, Codeigniter allows you to directly access any Controller/Method from the URL.
It also provides the creation of custom routes so you could have 10 or more urls all pointing at the same controller/method with parameter passing if that was your desire...
So in the case you ONLY want access to any Controller/Method that are defiend in the Routes config.
You need to test if the url is defined in the routes config array.
The main code is something like...
$this->load->helper('url');
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}
And you would put this in your controller's constructor you want to protect.
Of course you could create a common controller and extend those controllers you want to protect in this manner.
( NOT Recommended ) Or if you want to get really "hacky" you could put it in the system/core/controller constructor and make it system wide. SO Everything needs to be defined in a route.
NOTE: This breaks the 'default_controller'.
I'm using Codeigniter. Basically what I want is to remove the Controller name (Home) from the url.
Those urls look like:
http://localhost/mechanicly/Home
http://localhost/mechanicly/Home/about
http://localhost/mechanicly/Home/contactus
now there are two ways I can remove the Home controller:
static definition of every url inside route:
$route['about'] = "Home/about";
$route['contactus'] = "Home/contactus";
I can use call backs in routes in Codeigniter:
$route['(.+)'] = function ( $param ) {
if( strpos( $param, "Admin" ) !== false ) {
echo $param;
return "Admin/".$param;
}
else{
echo $param;
return "Home/".$param;
}
};
this logic is much better as it is generic and I don't have to create new routes every time for new method inside the controller.
It is working fine for the client controller which is Home but I have another controller named as Admin and I want to redirect Admin requests to the Admin controller and Home request to the Home Controller.
Why does above code work fine for the Home controller but returns
not found
when I validate for the Admin controller?
I am using CI version 3.x
If you really want to get crazy, you could parse the methods from the controller file and programatically create the "static" approach.
Pseudo code here
$controller_file_contents = file_get_contents('path/to/controller.php');
$controller_methods = function_that_parses_methods_from_file($controller_file_contents);
foreach ($controller_methods as $controller_method) {
$route[$controller_method] = "Home/" . $controller_method;
}
How function_that_parses_methods_from_file works is probably gonna involve a regex, something like function \w+. If you go with this approach, try to keep the controller as small as possible by offloading as much logic as possible into models, which is often a good idea anyways. That way the performance impact in the router is as small as possible.
Alternatively, you may be able to parse the controller using get_class_methods if you can figure out how to load the controller into memory inside the router without conflicting when you need to load the controller using the router or causing too much performance issues.
Pretty goofy, but every method you create in that controller will automatically create a route.
you can create your menu(urlĀ“s) from db like
tbl_menu tbl_level
---------- -------------
id id
fk_level level
name dateUP
dateUP active
active
In your controllers you need to call the correct menu by session or wherever you want
then you can has this in your route.php
$route['(.+)'] = 'int_rout/routing/' . json_encode($1);
in your controller Int_rout.php
public function routing ( $param ) {
$routing = json_decode($param);
$routing = explode('/', $routing);
//$menu -> get menu from model
foreach($menu as $item){
if($routing[0] === $item->name){
//$level -> get level from model
$redirect = $level->level;
}
}
//the final redirect will be like
//admin/user or admin/user/12
//public/us
$params = ( empty($routing[1])) ? '' : '/' . $routing[1];
redirect($redirect . '/' . $routing[0] . $params, 'refresh');
}
Some context: I've setup my Laravel 5 app to be split into modules. The boot() function in my AppServiceProvider looks like this:
public function boot()
{
// We want to register the modules in the Modules folder
$modulesPath = app_path() . '/Modules';
$handle = opendir($modulesPath);
// Loop through the module directory and register each module
while (($module = readdir($handle)) !== false) {
if ($module != "." && $module != ".." && is_dir("{$modulesPath}/{$module}")) {
// Check if there are routes for that module, if so include
if (file_exists($modulesPath . '/' . $module . '/routes.php')) {
include $modulesPath . '/' . $module . '/routes.php';
}
// Check if there are views for that module, if so set a namespace for those views
if (is_dir($modulesPath . '/' . $module . '/Views')) {
$this->loadViewsFrom($modulesPath . '/' . $module . '/Views', strtolower($module));
}
}
}
}
The idea is to be able to keep things separated in modules, but also have global routes and a global controller. Therefore, each module has its own routes.php file that looks something like this:
<?php
Route::group(array('module'=>'MyModule','namespace' => 'NexusHub\Modules\MyModule\Controllers'), function() {
Route::resource('mymodule', 'MyModuleController');
});
I then have a global routes.php file that looks like this:
<?php
Route::any('{catchall}', 'GlobalController#myAction')->where('catchall', '(.*)');
Route::group(array('module'=>'Global'), function() {
Route::resource('', 'GlobalController');
});
The problem I'm running into is that it seems my catchall route isn't picking up for the modules. The modules run their own routes but the catchall route is ignored.
As far as why I'm trying to accomplish this, for now the purpose is that all modules use the same layout, and that layout requires some data to be retrieved always, so the global controller would grab what's needed and make it available to the layout. But I suppose there may be some other things in the future where having a global route file that can catch multiple different routes based on arbitrary rules and run additional code would come in handy.
UPDATE: Removed the line that included the global routes since I realized they already got included by default anyway.
Your global routes file is being loaded first.
Try moving your service provider that loads all your module routes before the "App\Providers\RouteServiceProvider" in the config/app.php file.
...see if that helps.
I ended up doing what I wanted using middleware instead (see https://laravel.com/docs/master/middleware#global-middleware).
In my case I used something similar to the BeforeMiddleware class example and registered it in the $middleware property of my app/Http/Kernel.php class since it's global and not route dependant.
I want address of my website user is mydomain.com/username/. But it seems very difficult to me using codeIgniter.Please help me what is best way to do it.
mysite.com/profile?user=username
is current url of profile but i want this
mysite.com/username
please help me and I'm not good english speaker so I'm sorry if you not understand my question.
In your routes.php file route every existing controller (you should have atleast one) to itself. For example if you have controller main in main.php file route it:
$route['main'] = "main";
$route['main/(:any)' = "main/$1";
The reason why you should route it twice is because you must make sure that opening http://yoursite/main works as well as http://yoursite/main/my_method/ etc. Do this for every other controller you have.
The next step is to route everything else to your users controller. For example you have a profile method that has 1 argument - the username.
$route['(:any)'] = "users/profile/$1";
So now you will have everything else routed to users/profile/username.
One thing to remember is that the topmost priority goes higher in the routes.php file so your routes file should look something like:
$route['main'] = "main";
$route['main/(:any)' = "main/$1";
$route['users'] = "main";
$route['users/(:any)' = "main/$1";
$route['(:any)'] = "users/profile/$1";
See if that works!
You can use the CI routing
In your route.php file, select all your users and create a route for each :
$aUsers = $this->oDb->getAllUsers();
foreach($aUsers as $oUser) {
$route[$oUser->username] = "profile/" . $oUser->username;
}
It should work.
How can i create a vanity url in codeigniter. Im having real trouble doing this in the framework. And there doesnt seem to be any good answers out there.
It's possible, I'm using this in one of my projects...
Here is a thread on the CodeIgniter forums showing how to do it...
http://codeigniter.com/forums/viewthread/113962/
According to Personalized User Vanity URL's in CodeIgniter, you can accomplish this by modifying your routes file:
$handle = opendir(APPPATH."/modules");
while (false !== ($file = readdir($handle))) {
if(is_dir(APPPATH."/modules/".$file)){
$route[$file] = $file;
$route[$file."/(.*)"] = $file."/$1";
}
}
/*Your custom routes here*/
/*Wrap up, anything that isnt accounted for pushes to the alias check*/
$route['([a-z\-_\/]+)'] = "aliases/check/$1";