I want to use codeigniter for an ecommerce project I'm working on but I think I need some custom routing and I'm not sure if this is possible. I want to be able to use this url:
http://myecommsite.com/store/mens
By default in CI this would call the mens function in the store class. What I actually want is it to call a generic function in the store class and feed in 'mens' as a paremeter. The reason for this is that this site needs to have a mens,womens and childrens section.
Is this possible?
Also When I get further down the line...i.e
http://myecommsite.com/store/mens/category1/category2
how do I make Ci work with this?
Simply define a custom route in application/config/routes.php
Something like, for your url http://myecommsite.com/store/mens
$route['store/(:any)'] = "store/customfunction/$1";
This way all requests will be mapped to your "customfunction" method, which takes the parameter "mens"
You might also want to consdere the __remap() function, which overrides the methods (as opposed to the routing, which overrides the whole URI) Quoting from the manual:
If your controller contains a function named __remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
So you can use a __remap() function in your controller store, and anything will be redirected to that. Any segments after the method name are passed into __remap() as second parameter, and you can use this array with call_user_func_array().
This could come in handy for your second examples of URI. Might be something like
function __remap('mymethod',$array = array())
{
return call_user_func_array('mymethod',$array);
}
and in your method "mymethod" you pick the array element and do what you need to do
Related
I have a project that is sharing much of the same functionality across multiple clients use, each with their own sub-domain to access and use. For each instance, I'd like to add the odd bit of additional formatting or information to some views and was hoping to check whether a file exists upon rendering the view.
For example:
Let's say I have three different instances of the product (so three subdomains) and when a user logs in they view /dashboard by default. If the view is rendered from views/dashboard.blade.php, I'd like to check whether there is a file called views/subdomain.dashboard.blade.php and if so, use that as the view instead. I'm thinking this may be possible using the View Composer but not entirely sure how as still getting up to speed with Laravel.
You can make a helper for that.
if (!function_exists('subdomain_view')) {
function subdomain_view($view = null, $data = [], $mergeData = [])
{
$subdomain = request()->route()->parameter('subdomain');
$altView = "{$subdomain}.{$view}";
return view(
view()->exists($altView) ? $altView : $view,
$data,
$mergeData
);
}
}
It gets the subdomain, prefixes it to the view name, check if the prefixed view exists. If it does it uses that view, otherwise it uses the original view. It also uses the same signature as the view() helper.
Note that it makes use of the request() and and view() helpers without parameters. The default behavior of these helpers when called without parameters is to return the an instance of Illuminate\Http\Request and Illuminate\Contracts\View\Factory, respectively.
Another way to make this work is to provide your own implementation of Illuminate\View\ViewFinderInterface. You can check the Illuminate\View\FileViewFinder class that implements this interface. You can just subclass from it and override the find() method. Then register the new class.
Update: You don't need to even subclass FileViewFinder, just tweak how FileViewFinder is registered inside the service provider. Other solutions include adding view namespaces. See here.
I'm just new to Laravel but I immediately fell in love with it. As a not so super experienced php developer I do find the official documentation, although very expansive, somewhat complicated to use and find everything I need.
My question is about the Routing component. As the documentation states you can assign a route to a controller with the Route::controller method. So if I want a Blog controller for all /blog/ routes I assign it like this:
Route::controller('blog', 'BlogController');
So then if I'd like to acces all my blog posts I acces the the getIndex method by www.foo.com/blog or www.foo.com/blog/index
But let's say I'd like to be able to display categories via a getCategory method. My url would look like www.foo.com/blog/category and if, for example, I want to get the news category from the DB by slug, I'd like to use: www.foo.com/blog/category/news as the URI.
My question now is, how do I pass the slug to the url and access it in the getCategory method? Do I need specify it via Route::get('blog/category/{slug}', 'BlogController#getCategory') or is there a way to use Route::controller('blog', 'BlogController') and to send and acces parameters from the URL in the getCategory method?
I already tried to find it via google and in the official documentation, but I couldn't find a crystal clear answer to this problem...
You can simply add parameters to your getCategory method:
public function getCategory($category) {
die($category);
}
If you initialize it to null in the parameter list, it becomes optional. Alternatively, you can always pull parameters from the Input object but they would need to be passed in querystring format:
$category = Input::get('category');
With that said, I'd caution against using the Controller route. It's handy and mimics traditional MVC frameworks, but I believe it's planned to be deprecated -- and honestly, you miss out on some pretty flexible features.
using Route::controller('blog', 'BlogController'); allows you to define a single route to handle every action in a controller using REST naming conventions.then you have to add methods to your controller, prefixed with the HTTP verb they respond to. That means if you have a method called getIndex() it will be executed when there is a GET request to the url "yoursite.com/blog".
To handle POST requests to the same url add a method prefixed with post(ex: postComment()) and so on for other http verbs PUT, PATCH and DELETE.
I think you want something more customized, so you can use a resource controller:
Route::resource('blog', 'BlogController');
This will generate some RESTful routes around the blog resource, run php artisan routes in your project folder to see the generated routes, it should be something like this:
Verb Path Action Route Name
GET /blog index blog.index
GET /blog/create create blog.create
POST /blog store blog.store
GET /blog/{blog} show blog.show
GET /blog/{blog}/edit edit blog.edit
PUT/PATCH /blog/{blog} update blog.update
DELETE /blog/{blog} destroy blog.destroy
in the action column are the functions that you should have in the controller.
If you want to define more routes you can simply do it with Route::get or Route::post in the routes.php file
I hope this will make it more clear for you, enjoy routing with Laravel!!!
In Kohana 2, a controller function could have arguments in it without needing to write a route for it.
url: /some/arg/is/here
and in the controller, i could simply have four args, of any name, and they'd be automatically accessible from within the function.
public function myFunc($a, $b, $c, $d) {}
but in Kohana 3, I have to go write a route for type of route I want to have. Is there a route I can use that will make my url and args play nice with each other without me having to do extra work each time i write a new function?
All args must be specified in the route, but can be made optional. In this instance you may want to change the default route to something like...
Route::set('default', '(<controller>(/<action>(/<arg1>(/<arg2>(/<arg3>(/<arg4>))))))');
You can then reference the args by using "request"...
$this->request->param('arg1');
$this->request->param('arg2');
$this->request->param('arg3');
$this->request->param('arg4');
You could obviously have more than 4 if you needed them.
FYI Kohana 3 is a complete rewrite from scratch. Kohana 2 and 3 could be considered two separate frameworks.
As for the route, you maybe want to use a catch-all route. Check it out here Kohana 3.3 catch-all route
I would advise against using it, because it looses the whole purpose of KO3 routing flexibility.
Also, you can't access parameters as method arguments anymore since KO 3.1 (I think). Instead use the Request class to retrieve the parameters like so: $this->request->param('abc'); or instantiate the Request class if you're using it outside the controller like so Request::current()->param('abc');
This is the most simple way I can ask this question as I have not fully understood what's going on, or what I am not doing right.
I'm having trouble with the url.
http://localhost/index.php/user is the same as http://localhost/
but
http://localhost/index.php/user/something is not the same as http://localhost/something
How do I make http://localhost/something work?
Does it have to be http://localhost/user/something, how do I make that work?
You need to understand how CodeIgniter's URLs work.
An URL consists of some segments. http://localhost/index.php/user/something/thing In this example user, something and thing are segments of the URL.
Segments of the URL indicate which controller and which method of that controller will run. http://localhost/index.php/user/something/thing In this example the method something from user controller is called and thing is passed to that method as a parameter.
The first segment of the URL indicates the controller.
The second segment of the URL indicates the method of that controller.
The following segments are sent to that method as parameters.
But there are some defaults.
If your URL is http://localhost/index.php/something, you have something specified as the controller, but because you have not specified any method, the default method which is index is called. So the above URL is the same as http://localhost/index.php/something/index
If your URL is http://localhost/index.php/, you don't have any segments specified (no controller and no method). So the default controller which is specified in application\config\routes.php is the loaded controller. Which method of that controller will be called? Of course the index method.
--You can set the default controller by changing $route['default_controller'] = "site"; to what ever fits your application in application\config\routes.php's file.
If you want http://localhost/user/something to be the same as http://localhost/index.php/user/something, you have to create custom routes for your application. More info on that here.
http://localhost/something indicates that you are calling the index method of the Something controller class
http://localhost/user/something indicates that you are calling the something method in the User controller class.
Does that make sense?
In order to make http://localhost/something work, you need a controller called something with an index method. This would be the same as accessing http://localhost/something/index.
Alternatively, http://localhost/user/something implies that you have a user controller with a method called something.
Does that help at all?
To remove index.php from your URL you have to use the mod_rewrite method described here
Then to remove the controller name (user) from the url, you need to use routes
In your case, you would add $route['^(something|something_else|etc)(/:any)?$'] = "user/$0"; to your routes.php file
How to implement keyword specific page invocation using PHP codeigniter
Binary search Tree Implementation.
for e.g. in above URL keyword "binary-search-tree-implementaion" is method name or parameter for specific controller. because most of things are dynamic then how web site is going to manage all those things?
I want to implement it for my web site like this
http://example.com/search/digital-camera-price-in-india
I'm not familiar with CodeIgniter, but usually a URI structure like /search/digital-camera-price-in-india would route to the Search controller and digitalCameraPriceInIndia action.
/search/digital-camera-price-in-india
=> SearchController::digitalCameraPriceInIndiaAction()
If you want to route similar URIs (different products for example) to a catch-all method, you've have to setup custom routing.
The CodeIgniter documentation for routing is here
As per #chriso's answer you will need to set up a custom route to achieve this as by default the uri structure is /controller/action/params. So in your config/routes.php file you can add something like:
$route['search/(:any)'] =
"search/some_action";
And then use the relevant uri segment (
$this->uri->segment[1]
I think) as your search parameter.
if you set up your route like this:
$route['^search/(:any)'] = "search/my_controller_function/$1";
Then you can just write your function like this:
public function my_controller_function($search_input)
{
// your code here
}