Routing codeigniter with unlimited params - php

I have a controller Categories with action index and params(params will contain category name) which makes my url http://localhost/categories/index/(category_name)
I want my url to be written like this
http://localhost/(category_name)
But i also have sub categories and more deep level sub categories. So now my url should be written like this
http://localhost/(category_name)/(sub_categories)
or more deep level
http://localhost/(category_name)/(sub_categories)/(sub_sub_categories)
Is there a way to route my url to unlimited params of the action.
The below code is working
$route['(.+)'] = 'categories/index/$1';
but it affects to all the controllers and no other controllers seems to be working
I want to know is there another way to achieve the same.
Thanks!

You can use uri_to_assoc(). Check this page.

Related

Subcategories and resource controllers?

Im using a resource controller to control articles.
/news
Hits the index method and brings up all the articles under the category.
/news/article-slug
Hits the show method and brings up an article under the category.
My question involves sub categories.
/news/sub-category-name
This would hit the show method which would look for an article with the sub-category-name when I want it to hit the index method and bring up a list of articles for the sub category.
What would be the approach to use for sub categories?
Add another route and method like:
get('/news/{subcategory}', [
'as'=>'subcategory',
'uses'=>'ArticleController#getSubCategory',
]);
Or would it be better practice to scrap sub categories and have a url like:
/news-sub-category-name/article-slug
Or is there another way?
Thinking semantics you would be better to servers category names then the title. ie
www.domain.com/news/category/article
This will also give you a clean approach for your routes file. IE no wildcard matching or regex to replace (cat-) etc.
Thats the way I would personally go.
It use to be in the past you would want to keep your URL's as short as possible but thats just not the case these days. Dont get me wrong thats not to say you want domain/cat/cat/cat.....
... Update
To use resources you would have something like.
Route::resource('news/{cat}/article', 'ArticleController');

Add custom (parameter) route to an existing Magento CMS Page

I need to add an URL parameter to an existing CMS page. I basically created a categories overview page by creating a CMS page with the URL /artists (in my setup artists are equal to Magento's categories).
Now I need to create a custom/parameter route since I need a category-detail-page.
Example: fooshop.com/artists/artistname
where /artists lists all existing artists (categories) and /artists/artistname just shows one artist (detail) page.
One way to do so would be creating a simple URL rewrite (htaccess) to map all requests to /artists/someparameter to /artists/ but I was wondering if there's a more elegant solution by using Magento's default controller/view structure?
I ended up in writing a custom module that uses a custom controller that extends Mage_Core_Controller_Varien_Router_Abstract.

Need a light understanding MVC pattern

You have the URL:
http:///www.site.com/controller/method/values/1/2/3
Do I have to always have the controller being called or can I have the view being called and instantiate the controller inside the view or in a bootstrap file referring to this view?
What I don't get it is if I need more than 1 controller on the view, how to archive that?
For example:
On my index page I want run a simple CMS, where the admin can change the text blocks and images of the site. That would be on the content management controller.
On my index page I also got the latest added products vitrine, what would be controlled by the controller products.
If I define www.site.com/contentmanagement or www.site.com to run the contentmanagement controller, how the product controller would be called?
Also, another example. On my menu I got a link to a page called aboutus, that would be a simple page and the only feature needed would be the content management controller to manage the texts blocks.
If I follow the pattern Im reading all over the place I will end with a link like:
http://www.site.com/contentmanagement/method/aboutus
?
Kinda lost here cause surely this URL will look weird. Would be much easier to have the URL calling the view http://www.site.com/aboutus and a boot file where I can tell the controller that should be loaded when the surfer is there ...
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
I appreciate any help or a light here, thanks.
by the way, Im coding in PHP.
Hm, if you want to have text blocks and images of the site (one controller), and products vitrine (second controller), then call the methods you need in one controller..
I would do it this way: when you request index page with all the elements you mentioned above, actually one controller is called, and it decides which view to show.. so in that controller, you call the all methods that you need, get the data, and pass it to the view.. if the methods are called from other controllers, just call that methods and get the data.. i often make some static methods in controllers, which I can call anywhere, so I don't have to instantiate whole object..
E.g. you call www.site.com/contentmanagement, controller is called, which will display index view, and in that controller, you call all methods you need, prepare the data, and pass that data to the final view which will be displayed..
Do I have to always have the controller being called or can I have (..blah blah..)?
It kinda depends on what you understand by "call".
Controller needs an ability to change state of both View and Model(s).
And each View need ability to request data (in Model2 MVC) from Model(s).
Thought Controller should not cause the rendering of View (which is common in all the RoR sycophants) much like View has not business in executing actions on the Controller.
What I don't get it is if I need more than 1 controller on the view, how to archive that?
Views and Controllers should have 1:1 relationship. If your view need more then one controller, you might want to look into HMVC architecture.
For example: On my index page I want run a simple CMS, (.. more blah .. ) how the product controller would be called?
CMS should be a separate application ( or at least module ) with multiple controllers.
If I follow the pattern Im reading all over the place I will end with a link like: http://www.site.com/contentmanagement/method/aboutus ?
Why not just http://site.com/cms/content/2/edit (where 2 is the ID for "about us" page). There is no law which states that URL structure for site administration must mirror the publicly available page .. hell .. it can actually cause increased vulnerability.
I am going to try to walk
What I don't get it is if I need more than 1 controller on the view,
how to archive that?
For example: On my index page I want run a simple CMS, where the admin
can change the text blocks and images of the site. That would be on
the content management controller. On my index page I also got the
latest added products vitrine, what would be controlled by the
controller products. If I define www.site.com/contentmanagement or
www.site.com to run the contentmanagement controller, how the product
controller would be called?
Having the URL to contains the controller's name is definitely nice, but it is not the requirement for MVC. So you don't have to necessary map your controller to the URL itself. A classic MVC does not tie itself to a particular naming convention, so you could call your product controller via different URL which then process the product and show the view for the product.The important for MVC is to have one controller that deals with sets of model and resulting in one view as the presentation layer. In your particular example, your entry point seems to be a single controller that interacts with both ContentManagement and Product Class/Module and the resulting interaction produce a single view.
bootstrap would look like:
switch($view)
case: index
controller load contentmanagement
controller load product
case: aboutus
controller load contentmanagement
Thus your original interaction above is not completely off, except that you are not really calling two controllers, but rather doing the following upon hitting index:
Load a controller, let's call this one IndexController
Load ContentManagement module to get the related content, you might want to put the info as part of your Model for the Index page
Load Product module to get related products, again put this into your Model
Pass the Model containing the info for rendering the page into the View
In your View, you render the Model that contains both Content from ContentManagement module and Product list from the Product module thereby producing a single view
If I follow the pattern Im reading all over the place I will end with
a link like: http://www.site.com/contentmanagement/method/aboutus ?
This is again not a requirement, you should follow what makes sense and easier for the users. Also, if you are required to follow the conventions, you could always make your URL pretty by using URL mapping.
What you are looking for is called HMVC and there are a couple of frameworks for that out there, like Kohana.
Also see this question:
What is the HMVC pattern?

block access to certain pages in magento

i have a category in my store, lets call it Program, this category will only have 1 product, lets call it Program product, so, i've developed a module only to show this category and product, the view is diferent from the others categories, so, if i write in my browser the url
myweb.com/index.php/program.html or myweb.com/index.php/program-product.html i can see the custom categories page or the product view page and i don't want that, i want that if someone write this url he will be redirect to another one, how can i do that, if it is posible
thanks
"Do you want the category to show in the category menu but link to your new URL and not show in the old one?"
"yeah, this is exactly what i want, is this possible??"
Try this; Go to the menu Catalog > URL Rewrite Management and find the entry for your category. Crucially it's target path will look something like "catalog/category/view/id/123" ('catalog' is the module, 'category' is the controller, 'view' is the action and 'id/123' is a parameter).
Follow these URL rewrite instructions to delete the above entry then create a replacement. It's only difference will be the target path, use your module's path along with whichever controller, action and parameters are suitable for this case.
By preserving the ID Path of the old rewrite rule you are ensuring it doesn't get overwritten, although I haven't tested this with rebuilding indexes.
Does the category's enabled setting, and the product's visibility setting affect your module's display? If not use those settings to hide them from the catalog but continue using your module as normal.

zend framework - creating controller for admin/action1/action2

In zend framework, how do i create an action for this type of url:
example.com/admin/create/category
which would show a page for creating a new category
or
example.com/admin/edit/category/id
which would show a page to edit a category
here, admin would be the controller, create and edit would be the action but what about the last parameter 'category'? should i check for 'category' argument inside the controller actions or is there another way ?
thanks
Having this kind of issue, I suggest using zend route. Here's the link
http://framework.zend.com/manual/en/zend.controller.router.html
You can create multiple routes for each action if needed.
I think that the good way is to check for 'category' argument inside the controller actions. Based on its value you do what you want.
Assuming you have lots of different "things" you need to administer then i woudl suggest not using ana single admin controller but rather a Category controller. Then just secure the admin actions. Alternatively you sould have 2 controllers a Category controller and an AdminCategory controller...But either way you should have multiple controllers for the admin module....
Also keep in mind you can set up routes pretty much however you like... not every segment in the url needs to map to a parameter...

Categories