How to design a User Friendly URI in CodeIgniter? - codeigniter

I'm kind of new to CodeIgniter.
I need to design a simple website where I pull out information from a Database. The information from the database is about Ads. I have information about the Country, Country Zone, Zone Location and the kind of the Ad.
My goal is to have an URI like:
http://www.someurl.com/index.php/country/country_zone/zone_location/ad_type/title_of_ad
There are some information out there on how to do this with CodeIgniter?
Best Regards,

I think the easiest way is with a custom route in your application/config/routes.php. Something like this:
$route['^(:any)/(:any)/(:any)/(:any)/(:any)'] = "your_controller/get_ads/$1/$2/$3/$4/$5";
then your controller function would look something like:
function get_ads($country, $country_code, $zone_location, $ad_type, $title_of_ad) {
// your code
}
.. you can replace (:any) with a more specific regular expression if there is some pattern to the arguments.

the first 2 arguments in your link are your controller name and the method of that controller you want to call.
so
http://www.someurl.com/index.php/country/country_zone/zone_location/ad_type/title_of_ad
would mean that you call the country_zone method of your country controller and pass zone_location, add_type and title_of_ad to that method.
what you want ist
http://www.someurl.com/index.php/TheControler/themethod/country/country_zone/zone_location/ad_type/title_of_ad
implemented like that:
class TheControler extends Controler{
function themethod($country,$country_zone,$zone_location,$ad_type,$title_of_ad){
//your code
}
}

Related

How to call method dynamically on routes on Laravel?

I'm a newbie with this and I need some help.
I'm developing some kind of music library and let's say I don't want to make a route for each artist so I have made this one:
Route::get('/{artist_name}', 'Artist_controller#{artist_name}');
I get the value of {artist_name} from my view and the route works, for instance, the artist_name may be John and the url generated is localhost:8000/John. But when it comes to look for the class in the controller it doesn't work. I have a class named John in my controller, but I keep getting this error when I try to access:
BadMethodCallException
Method [{artist_name}] does not exist.
So I guess the route isn't taking the value of {artist_name}. What I intend is the route to be processed like:
Route::get('/John', 'Artist_controller#John');
But as I said, I don't want to create a specific route for an artist.
I'd appreciate any kind of help. Thank You
There is no need to create a dynamic method for each artist. You could have one generic method in your controller that handles retrieving the proper artist information from the database and pass it to the view.
routes file:
Route::get('artists/{artist_id}', 'ArtistsController#show');
ArtistsController.php
class ArtistsController extends Controller
{
public function show($artist_id)
{
$artist = Artists::find($artist_id);
return view('artists.show', ['artist' => $artist]);
}
}
So if the user hits the following URL http://localhost/artists/4 the artist id of 4 will be passed to the show method and it will dynamically looks for an artist with that ID and pass an object of artist to your view.
Of course you are not limited to IDs in your URLs. You can use the name if it was unique and your code will be as the following.
routes file:
Route::get('artists/{artist_name}', 'ArtistsController#show');
ArtistsController.php
class ArtistsController extends Controller
{
public function show($artist_name)
{
$artist = Artist::where('name', $artist_name);
return view('artists.show', ['artist' => $artist]);
}
}
I suggest you read this documentation for more information about routing.
You can not have dynamic method (controller action) in a controller class. Instead you should define a method and pass the route parameter to that action.
In your route (web.php) file:
Route::get('/{artist_name}', 'ArtistController#artist');
then in ArtistController.php:
public function artist ($artist_name) {
// do stuff based on $artist_name
}
To get more info read these 2 documentation pages. Controller and Routing.

Laravel Routes and 'Class#Method' notation - how to pass parameters in URL to method

I am new to Laravel so am uncertain of the notation. The Laravel documentation shows you can pass a parameter this way:
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
Which is very intuitive. Here is some existing coding in a project I'm undertaking, found in routes.php:
Route::get('geolocate', 'Api\CountriesController#geolocate');
# which of course will call geolocate() in that class
And here is the code I want to pass a variable to:
Route::get('feed/{identifier}', 'Api\FeedController#feed');
The question being, how do I pass $identifier to the class as:
feed($identifier)
Thanks!
Also one further sequitir question from this, how would I notate {identifier} so that it is optional, i.e. simply /feed/ would match this route?
You should first create a link which looks like:
/feed/123
Then, in your controller the method would look like this:
feed($identifier)
{
// Do something with $identifier
}
Laravel is smart enough to map router parameters to controller method arguments, which is nice!
Alternatively, you could use the Request object to return the identifier value like this:
feed()
{
Request::get('identifier');
}
Both methods have their merits, I'd personally use the first example for grabbing one or two router parameters and the second example for when I need to do more complicated things.

Get parameter from url in codeigniter?

My url is: localhost/school/DPC
In the above url school is my controller class and DPC is the school name. I want to get the name DPC from the url in the school controller class.
In routes.php page I have used
$route['default_controller'] = "school";
Now what do I have to do such that I can get the school name from the url.
I think you want to create a profile page like other website are using ... abc.com/hameed
right?
If yes then in Route you should also add this
$route['school/(:any)'] = "school/profile/$1";
And in your school controller you can do this
function profile($school_name){
echo $school_name ;
}
According to your question, " localhost/school/DPC " , it will be intepreted by codeigniter in the following way:
'school' as controller and 'DPC' as a function name of 'school' controller class.
Since you are expecting to send DPC directly through url, you should make one function inside school class say 'myFunction' with a parameter say '$name' then your url will look like "localhost/school/myFunction/DPC" then only you can get your desired result.
Remember: the url format of CI is "hostname/controller/function/parameter". You are missing the function name that accepts that parameter.

How to make urls like category/5/sub_category in CodeIgniter

I want my urls to look like:
www.domain.com/catalog/category_name/category_id/product_name/product_id.
How should my controllers look like to accomplish this?
It's ok for the Controller to have Catalog and Function category_name in it.
But what will be my product controller and function. How can I make a structure like this.
Do I need to have a specific file structure?
I use CodeIgniter framework. Thanks for your help.
In general the controller must not be changed (if it accepts the product id as parameter).
All other information can then be put in the url from the database (querying via the product id and getting related information) through URI Routing changes.
your controller method should be like this
<?
//..
public function category_name($category_id,$product_name,$product_id){
// some cool code
}
//..
?>
As you question is very vague - so is my answer - but this function achieves what you asked
Class Catalog extends CI_Controller
{
public function category_name ($category_id, $product_name, $product_id)
{
// your function
}
}

filter a string to remove disallowed characters to compose an URL in CodeIgniter

I'm trying to make URL-friendly links for the blog on my portfolio.
So I would like to obtain links something like site/journal/post/{title}
Obviously Journal is my controller, but let's say my title would be 'mysite.com goes live!' I would like to have a valid url like site/journal/post/mysitecom-goes-live where all disallowed characters are removed.
How would I transform 'mysite.com goes live!' to 'site/journal/post/mysitecom-goes-live' in CodeIgniter based on the characters in $config['permitted_uri_chars']
use the url helper
$this->load->helper('url');
$blog_slug = url_title('Mysite.com Goes live!');
echo $blog_slug //mysitecom-site-goes-live
// might differ slightly, but it'll do what you want.
to generate url-friendly links.
Store this value in a field in your blog table (url_title/url_slug) whatever.
make a function:
class Journal extends controller
{
//make your index/constructor etc
function view($post)
{
$this->blog_model->get_post($post);
// etc - your model returns the correct post,
// then process that data and pass it to your view
}
}
your blog_model has a method get_post that uses CI's
$this->db->where('url_title', $post);
hope that makes sense.
then when you access the page:
site.com/journal/view/mysite-goes-live
the function will pick up "mysite-goes-live" and pass it to the view() function, which in turn looks up the appropriate blog entry in the database.

Resources