Get parameter from url in codeigniter? - 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.

Related

laravel 5.8 edit function with model instance

public function edit(EduLevel $eduLevel)
{
dd($eduLevel->name);
return view('adm.edulevel.edit',compact('eduLevel'));
}
Route::resource('edulevel','EduLevelController'); //web.php
with resource route
how to get eduLevel to view with model instance laravel. in previous i call with parme parameter id and use find() method to get data..
from this sample - https://itsolutionstuff.com/post/laravel-58-crud-create-read-update-delete-tutorial-for-beginnersexample.html
I don't understand the question but I will just guess that you have a route that accepts a parameter that you you expect it to be the model inside your function.
You need to create a route like this one:
Route::get('/edit/{eduLevel}', 'SomeController#edit');
Notice the same name for the variable, this is important otherwise you will get only the id, slug or whatever.
Make sure your path name also have the same name for route segment name.
so your route path should be like this.
Route::get('/edit/{variablename}', 'ControllerName#edit');
your controller function logic should be like this.
public function edit(EduLevel $variablename)
{
return view('adm.edulevel.edit',compact('variablename'));
}
So make sure your variable name in route and in controller function
should be same.
For more information, you can read Route Model Binding in laravel
I am having the same problem (almost).
I wanted to call a controller method in the view. So I should pass the model from controller to view.
How to pass model from controller to view?
I found this [Laravel 5 call a model function in a blade view but using ->withModel($model); to pass the model from controller to view and {{$model->someFunction()}} to call the method in the view is not working.
Any advice please?

Want to show name instead of id in the URL field in Laravel

I don't want to show /route_name/{id} in the URL field of my Laravel project. Instead of that I want to show /route_name/{name} but pass the id in the back-end to the controller.
Suppose I have a route named departments and pass an id 3 named knee_pain as a parameter. And it is like /departments/3
But I want to to show /departments/knee_pain in my url and as well as want to pass the id 3 in my controller without showing the id in the url.
How to do that ?
In your model you can use the getRouteKeyName method to bind to another attribute than the default id in your routes :
public function getRouteKeyName()
{
return 'slug'; // Default is 'id'.
}
Rather than using the name attribute, that you could use elsewhere in your application for displaying the name of the entry, I recommend using an attribute made url friendly. You could use Str::slug() for that.
public function setNameAttribute($value) {
$this->name = $value;
$this->slug = \Str::slug($value);
}
It will 'slugify' your string, for example : \Str::slug('Knee pain') => 'knee-pain'.
Note : in Laravel 5.5, use the str_slug() helper.
You should also make sure this string is unique in your database.
First you have to garantee that the name is unique, if don't you will have more than one Id in your controller. For that i recommend you to use Purifier to remove spaces and make it URL friendly:
Purifier
Second, probably the best way to have clean controllers is creating a middleware that understand what kind of name is (what table should middleware look for). You can validate that by route name and send the correct id to controller.
Middleware docs

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.

How to make my codigniter url seo friendly

How to make my codigniter url seo friendly.
this is my url
http://localhost/picker2/web/search?category=doctor
i want url like this
http://localhost/picker2/web/search/doctor/
read this article www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
to create url like this : http://heloo.com/article/codeigniter-based-url
add this code in routes.php
$route['article/(:any)'] = "article/readmore/$1";
description :
article : class name
readmore : method from class article
$1 : get value from uri segment 2 value
Actually you have two options
1) Using Routes (this one already denyptw discussed right ?)
2) use the URL Helper url_title function
Note : You can use Parameter instead of query string
http://localhost/picker2/web/search?category=doctor
http://localhost/picker2/web/search/doctor/
web controller , search function , doctor parameter
example :
class Web extends CI_Controller
{
public function search($value)
{
//use this $value in your searching logic
}
}
You need Example of URL Helper
check this link Codeigniter URL: How to display id and article title in the URL
you can do this using routes.(Elislab)
as a simple trick.(Using Controllers)
each and every page create a controller. (ex: if contact create contact controller, if product create product controller.) So each and every link will be look like(if you click product) www.example.com/product,(if you click contact) www.example.com/contact.
If you want something to do with your product then write that method inside the product controller.(ex if you create cart then URL show www.example.com/product/cart )

How to design a User Friendly URI in 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
}
}

Resources