Codeigniter HVMC modular seperation extension URL rewrite / routing - codeigniter

Ive been using HVMC modular extension, and its working great, but Im having trouble figuring out how to use, and if it is possible to use URL routing with HVMC.
Basically, I have a module called “site”, which is my main default site controller. All of the other modules I am not using directly, I am only using them by calling echo modules::run(‘controller/method’);—So basically I just want to remove “site” from the URL so that all the methods within the site module/controller appear without the word “site” in it.
Can anyone tell me if this can be done with HVMC modular extensions?
Any help much appreciated!

For completeness I have been researching my own solution to this issue and the removal of the "site" prefix on the URI string can be achieved by adding the following into the routes.php config file.
$route['(:any)'] = "site/$1";
$route['default_controller'] = "site";

I also worked since 3 years in CI HMVC, and some of my routing examples is there, it may help you.
I define 2 types of module here one is site and another is admin.
1>Routing for admin:
/*ADMIN is a constant, you can define anything like admin or backend etc. */
/*Example: admin/login*/
$route[ADMIN.'/([a-zA-Z]+)'] = function($controller){
return 'admin/'.strtolower($controller);
};
/*Example: admin/user/listing*/
$route[ADMIN.'/([a-zA-Z]+)/(:any)'] = function($controller, $function){
return 'admin/'.strtolower($controller).'/'.str_replace("-","_",strtolower($function));
};
/*Example: admin/user/edit/LU1010201352*/
$route[ADMIN.'/([a-zA-Z]+)/(:any)/(:any)'] = function($controller,$function,$param) {
return 'admin/'.strtolower($controller).'/'.str_replace("-","_",strtolower($function)).'/'.$param;
};
/*Example: admin/user/assign-group/LU1010201352/G2010201311*/
$route[ADMIN.'/([a-zA-Z]+)/(:any)/(:any)/(:any)'] = function($controller,$function,$param,$param1){
return 'admin/'.strtolower($controller).'/'.str_replace("-","_",strtolower($function)).'/'.$param.'/'.$param1;
};
2>Routing for site:
$route['([a-zA-Z]+)'] = function($controller) {
return 'site/'.strtolower($controller);
};
$route['([a-zA-Z]+)/(:any)'] = function($controller,$function){
return 'site/'.strtolower($controller).'/'.str_replace("-","_",strtolower($function));
};
$route['([a-zA-Z]+)/(:any)/(:any)'] = function($controller,$function,$param) {
return 'site/'.strtolower($controller).'/'.str_replace("-","_",strtolower($function)).'/'.$param;
};
$route['([a-zA-Z]+)/(:any)/(:any)/(:any)'] = function($controller,$function,$param,$param1) {
return 'site/'.strtolower($controller).'/'.str_replace("-","_",strtolower($function)).'/'.$param.'/'.$param1;
};
It is totally dynamic. You can create lots of controller inside any module. If you want to add more module then you just make another block of routing like 1 or 2.

Related

Codeigniter 3.1.16 routing issues

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'.

How to render a cms page with default theme AND variables from controllers in OctoberCMS?

I'm wondering how I can render a view, or display a page with my default theme in OctoberCMS, via a route that executes a function in a controller.
If I have the following route:
Route::get('bransje', [
'uses' => 'Ekstremedia\Cityportal\CPController#bransje'
]);
And in my controller CPController ive tried several things, like I used to with Laravel:
public function bransje() {
$stuff = Stuff::with('info');
return View::make('cms::bransje')->with('stuff',$stuff);
}
But I cannot seem to get it to work, and I've tried to search the web, but it's hard to find answers. I have found a workaround, and that is to make a plugin component, then I can include that component and do:
public function onRun()
{
$this->eventen = $this->page['stuff'] = $this->stuff();
}
protected function stuff()
{
return ...
}
Is there any way so I can make pages without using the Cms, and that are wrapped in my default theme? I've tried
return View::make('my-theme-name::page');
and a lot of variants but no luck.
I know I can also do a:
==
public function onRun()
{
}
in the start of my page in the cms, but I'm not sure how to call a function from my plugin controller via there.
You can bypass frontend routing by using routes.php file in your plugin.
Full example in this video turotial.
If this answer can still be useful (Worked for October v434).
I have almost the same scenerio.
What I want to achieve is a type of routing like facebook page and profile.
facebook.com/myprofile is the same url structure as facebook.com/mypage
First I create a page in the CMS for each scenario (say catchpage.htm)
Then a created a catchall route at the buttom of routes.php in my plugin that will also not disturb the internal working of octobercms.
if (!Request::is('combine/*') && !Request::is('backend/*') && !Request::is('backend')) {
// Last fail over for looking up slug from the database
Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {
//Pretend this are our routes and we can check them against the database
$routes = ["bola", "sade", "bisi", "ade", "tayo"];
if(in_array($slug, $routes)) {
$cmsController = new Cms\Classes\Controller;
return $cmsController->render("/catchpage", ['slug' => $slug]);
}
// Some fallback to 404
return Response::make(View::make('cms::404'), 404);
});
}
The if Request::is check is a list of all the resource that october uses under the hood, please dont remove the combine as it is the combiner route. Remove it and the style and script will not render. Also the backend is the url to the backend, make sure to supply the backend and the backend/*.
Finally don't forget to return Response::make(View::make('cms::404'), 404); if the resource is useless.
You may put all these in a controller though.
If anyone has a better workaround, please let us know.

Code igniter url segment

currently my website url is
https://www.thefoolsbookie.com/main/inside?id=8
but I want this to be like this
https://www.thefoolsbookie.com/nfl
How can I do this?
Edit the application/config/routes.php file and add a new route for it
$route['nfl'] = 'main/inside';
It should be as simple as that :)
See https://ellislab.com/codeigniter/user-guide/general/routing.html for more examples.
Edit
I've worked with CI for a long time and I've never seen it use GET params in the routes file in that way. I'm not sure it is possible.
You could have it like $route['nfl'] = 'main/inside/8';
then in the main controller your inside method would look like:
public function inside($id)
{
//$id would contain the ID of 8 when you go to
//https://www.thefoolsbookie.com/main/inside/8
//or https://www.thefoolsbookie.com/nfl
}

How to create username in codeIgniter?

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.

CodeIgniter - When using $route['(:any)'] = 'pages/view/$1' how to use other controllers?

When using
$route['(:any)'] = 'pages/view/$1';
and I want to use other controllers in my routing for example:
$route['del/(:any)'] = 'crud/del';
it won't work. I guess it will use
pages/view/del/$1
and not my crud-controller when deleting an item. How can I solve this?
As indicated, $route['(:any)'] will match any URL, so place your other custom routes before the "catch-all" route:
$route['del/(:any)'] = 'crud/del';
// Other routes as needed...
$route['(:any)'] = 'pages/view/$1';
Its hundred percent working
$route['(:any)'] url is placed last in your routes file
$route['(:any)/company_product_deal_detail'] = "mypage_product_picture/deal_detail/$1";
$route['(:any)/company_service_deals/(:any)'] = "mypage_service_deal_list/index/$1";
$route['(:any)/company_service_deals'] = "mypage_service_deal_list/index/$1";
$route['(:any)'] = "company/index/$1";
I know that it's an old question, but I have found myself a nice solution.
By default, CodeIgniter gives priority to URL's from routes config (even if straight controller, method etc. specified), so I have reversed this priority this way:
In system/core/Router.php find _parse_routes method.
Add this code under literal route match:
$cont_segments = $this->_validate_request($this->uri->segments);
if ($cont_segments == $this->uri->segments) {
return $this->_set_request($cont_segments);
}
I agree, that this approach is kinda wrong, because we edit file from system/core, but I needed a fast soluttion to work with a lot of URL's.

Resources