Yii urlManager route with query string - php

This is about setting WebRoot/protected/config/main.php *urlManager* on Yii Framework.
I want a few predefined url like /About /Contact or some other custom special page like /LatestUpdates or /HTCDesire
I can do 'Contact'=>'site/contact' to map /Contact to /site/contact
How can I 'hard code' query string to the route?
I try:
'About'=>'site/page?view=about'
'LatestUpdates'=>'update/view?id=65'
'HTCDesire'=>'product/view?id=33'
but not working, 404 not found because it expect the right side should be route in format controller/action.
I try:
'LatestUpdates'=>'update/view/id/65'
I can browse to /LatestUpdates but the anchor link still shows /update/view/id/65
Is there any other way?

This works for me (except I substituted your values in, so maybe I broke it...):
'LatestUpdates'=>array('update/view','defaultParams'=>array('id'=>'65')),
More about this here and here.
Cheers!

Related

seo friendly url for codeigniter

We have a social networking web site for students. Our url looks like this domain.com/student/145236 where 145236 is the roll number. We now have an option of doing something like this domain.com/student/145236/student-name but since google / search engines would prefer domain.com/student-name/student/145236 we would like to do like this, but we are unable to do it, we tried doing this using routes $route['(:any)/student/(:num)'] = 'student/$1'; but it shows 404 page, any help would be grateful :)
This should be work for you:
$route['(:any)/student/(:num)'] = 'student/yourfunction/$1/$2'; 
Here $1 is for ist param (:any) and
$2 is for second param (:num) .
For getting values from the URL inside the student controller you can use CI Segments.

Codeigniter URL ReWriting Issue

I am working a codeigniter project. for example I have a controller Blog and a method category with param.
When i go to a link
Developer Blog
it works fine. which redirect to a page developer blog.
Is it possible to rewrite the url as domain.com/blog/developer-blog dynamically ? Thank you.
Open your application/config/routes.php and try following (if you want exact as mentioned in question)
$route['blog/developer-blog'] = 'blog/category/2';
$route['blog/user-blog'] = 'blog/category/3';
for dynamic routing you may use this
$route['blog/developer-blog/(:num)'] = 'blog/category/$1';
$route['blog/user-blog/(:num)'] = 'blog/category/$1';
so your URL now should be looks like https://domain.com/blog/developer-blog/1, https://domain.com/blog/developer-blog/2
or https://domain.com/blog/user-blog/1, https://domain.com/blog/user-blog/2 etc
for more see http://www.codeigniter.com/user_guide/general/routing.html

URL rewriting using Codeigniter for specific rule

On my local server, I have setup a website using Codeigniter which opens up fine using below URL
I have a different kind of query regarding rewriting URL
http://localhost/mywebsite
Actually the real URL is below
http://localhost/mywebsite/page/
Which I want it to work as root url as
http://localhost/mywebsite
So I have done it by adding it as default controller in config.php
But now I have another URL as
http://localhost/mywebsite/page/mypagename
Which should open as
http://localhost/mywebsite/mypagename
How can I do it?
You can define a custom route in /system/application/config/routes.php - for example:
$route['abc'] = 'controller_name/abc';
Then, http://mydemosite.com/abc
goes to http://mydemosite.com/controller_name/abc
see https://ellislab.com/codeigniter/user-guide/general/routing.html for more information of URI Routing.
I hope this can be helping you.
I got it solved using routing method as below under config/routes.php
$route['([^/]+)/?'] = 'page/$1';

codeigniter segment method

Im newbie here.I have a problem with codeigniter segment() method.I referred 6th segment of URL at href($this->url->segment(6)) but when i click the link it goes to full URl/6th segment.i.e
I want my link will be go here www.webcoachbd.com(this is 6th segment of my URL)
but it goes http://www.example.com/controller_name/method_name/segment1/segment2/segment3/www.webcoachbd.com
Are you doing this in your view?
I dont know if $this->uri->segment will work inside views, never needed it myself.
Instead grab the URI inside your controller and pass it back to the view.
$this->load->view('some view', array(
'link' => $this->uri->segment(6)
));
-
link
Although I dont really understand why you would want to do that.
Its not url but uri
$this->uri->segment(6);
and i think you are missing = operator in the anchor
MyLink
From the resulting URL that you posted, it looks like you might be missing a forward slash.
link text
This would force the URL to forward to the root of your site, rather than to that location inside the current folder that the browser thinks it is in.

How to solve the case when users surf to index.php

In Zend framework, using the MVC, if A user surf explicitly to http://base/url/index.php instead of just http://base/url, The system thinks the real base url is http://base/url/index.php/ and according to that calculates all the URLs in the system.
So, if I have a controller XXX and action YYY The link will be
http://base/url/index.php/XXX/YYY which is of course wrong.
I am currently solving this by adding a line at index.php:
$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);
I am wondering if there is a built-in way in ZF to solve this.
You can do it with ZF by using Zend_Controller_Router_Route_Static (phew!), example:
Read the manual page linked above, there are some pretty good examples to be found.
$route = new Zend_Controller_Router_Route_Static(
'index.php',
array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);
Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.
Well it really depends on how you want to solve this. As you know the Zend Frameworks build on the front controller pattern, where each request that does not explicitly reference a file in the /public directory is redirected to index.php. So you could basically solve this in a number of ways:
Edit the .htaccess file (or server configuration directive) to rewrite the request to the desired request:
RewriteRule (.*index.php) /error/forbidden?req=$1 // Rewrite to the forbidden action of the error controller.
RewriteRule index.php /index // Rewrite the request to the main controller and action
Add a static route in your bootstrapper as suggested by karim79.
Use mod_rewrite. Something like this should do it:
RewriteRule ^index.php/(.*)$ /$1 [r=301,L]
I don't think you should use a route to do this.
It's kind of a generic problem which shouldn't be solved by this way.
You better should have to do it in your .htaccess, which will offer you a better & easier way to redirect the user to where you want, like to an error page, or to the index.
Here is the documentation page for the mod_rewrite
I've never faced this problem using Zend Framework. just do not link to index.php file. that's it. and when your are giving your application's address to users, just tell them to go to http://base/url/
when the user enters http://base/url/ her request URI is base/url and your .htaccess file routs the request to index.php, but the request IS base/url. you do not need to remove 'index.php' from the request. because it is not there.
when you are trying to generate URLs for links and forms and ..., use the built-in url() view helper to generate your links. like this:
// in some view script
<a href="<?php
echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
?>" >click</a>
do not worry about the link. Zend will generate a URL for you.
The way I look at this is that if I have a website powered by PHP and a user goes to http://site/index.aspx then I would send a 404.
Even though index.php does exist in theory, it's not a valid URL in my application so I would send a 404 in this case too.

Categories