CodeIgniter with Wiredesignz HMVC routing - codeigniter

I am using HMVC and created a module called user. Inside modules/user/config directory, I have routes.php, using the same format as application/config/routes.php.
In application/config/routes.php I have the following route:
$route['login'] = 'user/login';
This works great, buit when I move it to application/modules/user/config/routes.php, it does not work. I get a 404 error.
According to HMVC docs (https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc), you have to create routes as follows:
$route['module_name'] = 'controller_name';
This means I will have to do it like this:
$route['user'] = 'user';
This causes the 404, however, even if I did not get the 404, this is not quite what I have in mind. I still want to keep my routing to work as /login goes to user/login.
Any ideas would be greatly appreciated!
Thanks!

I had the same exact problem as you, unfortunately the way Wiredesignz created the extension it requires that the path start with the module name itself if you put the routes file inside the module itself. That is the only way it will look at the routes file if it is placed inside a module. With that said it already knows the module name at that point, so you need to simply specify the controller and method that you want it to route to. So in your routes.php file inside of your module config directory, if you put this:
$route['yourmodule/some-route'] = "yourcontroller/yourmethod";
or in other words:
$route['user'] = 'user/login';
That would work I believe. However I still wanted more than this. I wanted to be able to use routes that may or may not have the module name. Due to this I had to extend the module to make this happen, and you can find the work I did here if this helps:
https://github.com/brianwozeniak/codeigniter-modular-extensions-hmvc
This would then allow you to use the route you wanted such as:
$route['login'] = 'user/login';
Even with that routes.php placed inside the module's config directory.

Related

My Laravel Routes Are Not Working Error 404

I am working on an existing laravel project. Problem is whenever I create a new route the new route does not function. Also I am not receiving this error
GET http://localhost/khawaja%20sons/khawjapos.unialsolutions.com/public/products/warehouse 404 (Not Found).
Routes
ProductController
link from where I am using the route
Output
You have a two option how to use routes after you are set them:
first one :
Link
You have tried this method but, you need to remove asset function, because asset function return path to public folder
The second way to use route and the way I recommend is to use route names
Link
I recommend this way, because if you change the path in future, your change will be applied to everywhere you use route name, you don't need to be worry is route will work
I have tried both options before. Both are giving the same error

Can only Use Library in Routes file

I've been testing out the Imagine Library in my Laravel application and have been running the code straight in the Routes file which has worked fine. However, now when I'm trying to use it in a Model or Controller, it can't seem to find the library anymore even though I've included it the same way I did before with use Imagine\Image\Box; and use Imagine\Gd;. The code is also exactly the same as in the Routes file but I still get class 'App\Imagine\Gd\Imagine' not found.
Do I have to do something different when including stuff in a Model or controller?

Getting a route to an external laravel instance

I have two separate Laravel instances/sites running on a server and want to be able to generate a url to a named route on one from code within the other.
For example the following named route exists in the first instance:
Route::get('users/my_account', array('uses' => 'UsersController#myAccount', 'as' => 'my_account'))
In the second instance I want to generate a url to the route above. Can anyone think of a clean way to do this, without explicitly knowing the url (i.e. only knowing the name of the route 'my_account')?
Basically I want to expose the RouteCollection of one site to the other...
That's a pretty interesting question. There's no natively supported way of doing that and, from what I know, it won't ever.
You could try loading the routes file of the first application, parsing it's configurations (you will need that for reverse routing), construct a Router instance and use it, but I'm sure it won't be simple at all.
If you have a really, I mean really, good reason to use reverse routes, you can try building a small API on the first application. That API should receive the parameters necessary, those used in url($params), and return the full url (with domain and everything). Although, this will introduce some serious performance issues.
IMHO, stick to hard coded my_account, leave a comment on the first application route and/or controller explaining that it's used on another project and move on :)
I may call this a dirty trick. Supposing you have the following structure in your file system, where both paths are Laravel apps:
/path/to/apps/app1
/path/to/apps/app2
And you want to load the routes file from app1 into app2. You can do it as follow:
include "../app1/apps/routes.php";
$url = URL::route('register');
VoilĂ ! But, although it worked for me there are some points to consider.
Include that file, will surely overwrites your current route collection for that process.
If that last is true, then you can have problem generating other URLs, maybe in your views.
The domain name will be of the current Laravel instance. It means that your URLs generated into app2 will hold the domain name of app1. I believe this is not what you want. But you can always generate non-absolute URLs with URL::route('register', null, false).

PHPUnit + CodeIgniter multiple objects with same name

I currently test my CodeIgniter app with phpunit by using CIUnit (https://bitbucket.org/kenjis/my-ciunit). The problem is that I have multiple controllers with the same name. I have a controller in the root controller directory named "Blog" and I have a controller called "Blog" in the controller/ajax/ directory.
The reason is to seperate all ajax requests from the main controller.
When I am running tests on both files, I get the following error:
PHP Fatal error: Cannot redeclare class Blog in ...
Well, I am not suprised I am getting this error.
What are my options to resolve this?
Prefix controllers in ajax directory with "ajax" (looks only a bit stupid url/ajax/ajax_blog)
Use namespaces (I guess I need to namespace codeigniter too then)
Create 3 seperate phpunit.xml files
This aren't really solutions I am looking for. Do I have any other options? Is it somehow possible to run each testsuite seperatly, but still in one command? Can I "clean" objects between testsuites? Anything else?
There are no other options except those you mentioned, as it is impossible to "unload" class definitions in PHP.
Naming two controllers the same is not a problem when you run CI normally, since only one controller is instantiated per request, but something that should be avoided.
If it is just the ajax-url you don't like, maybe override it in a route (in config/routes.php):
$routes['ajax/blog'] = 'ajax/ajax_blog';

Referencing script files from MVC

I am having problems referencing scripts that should be included in my view, when I access the page using a different route my scripts fail.
The idea is that two routes actually point to the same action:
http://localhost/PaydayWebsite/registration
http://localhost/PaydayWebsite/organizations/p001/departments/vest/employees/chn/registration
where the second just include more params for my action. I have tried using
ResolveUrl, Url.Content and mvccontribs Html.ScriptInclude but neither seem to work. Any ideas?
You should be able to use Url.Content, but you'll need to make your path relative to the root of the application rather than the current path.
Use
Url.Content( "~/Content/Scripts/jquery-1.3.2.min.js" )
rather than
Url.Content( "../../Content/Scripts/jquery-1.3.2.min.js" )

Resources