I have 4 controllers: Catalog, Contact, About, Main.
On each controller, I want to set the individual URL from the database.
The structure of the database page:
url_title|id_page|name_page|page_section|
about-us | 5 | About | about |
how can this be implemented, taking into account that the function will be also indicate the URL?
the site url can be different then the controller name and optional method name.
in application/config/routes.php
$route['about-us'] = 'about';
so then website.com/about-us will 'route' to the about controller index method
if you want to specify a method in the about controller such as show()
$route['about-us'] = 'about/show';
Related
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
Is there way of routing controllers in CodeIgniter such that a controller with a url like path/controllerA/some_method becomes /some_name/some_method_ofA where some_name is controller A without moving controller A?
I can do this properly with one method of controller a via:
$route['some_name/(:any)'] = '/path/controllerA/some_method/$1';
but I can't seem to get it so that some_name is basically an alias for controller A such that I can access other methods.
So before: path/controllerA/some_method_ofA, path/controllerA/some_method_ofA2
After: /some_name/some_method_ofA, /some_name/some_method_ofA
Yes, you can set it up:
$route['signin'] = 'Contact/SignIn'; //Maybe that, controller contact and method SignIn
$route['page/(:any)'] = 'Post/index/$1';
You just need to use:
$route['new_url'] = 'Controller/method';
I want to know is there any possibility to reduce routes for same controller in laravel4.
Here is my route:
Route::get('emp/add-employee/','EmpController#addEmployee');
Route::post('emp/add-employee/','EmpController#addEmployee');
Route::get('emp/edit-employee/{id}','EmpController#editEmployee');
Route::post('emp/edit-employee/{id}','EmpController#editEmployee');
Route::get('emp/view-employee/{id}','EmpController#viewEmployee');
is there any posibility to do reduce...?
Your route actions look like the ones you'd find in a RESTful Resource Controller. So you could use this:
Route::resource('emp', 'EmpController', array('only' => array('create', 'store', 'edit', 'update', 'show')));
This will of course require you to rename the controller methods accordingly and the route paths will be a little different, but you'd have a more compact route definition and consistent naming. Below are the routes that are generated by the Route::resource definition above.
+-----------------------------+---------------+-------------------------+
| GET emp/create | emp.create | EmpController#create |
| POST emp | emp.store | EmpController#store |
| GET emp/{id} | emp.show | EmpController#show |
| GET emp/{id}/edit | emp.edit | EmpController#edit |
| PUT emp/{id} | emp.update | EmpController#update |
+-----------------------------+---------------+-------------------------+
So you'd have to rename your controller method names like so:
GET : addEmployee() -> create() // shows the add form
POST: addEmployee() -> store() // processes the add form when submitted
GET : editEmployee() -> edit() // shows the edit form
POST: editEmployee() -> update() // processes the edit form when submitted
GET : viewEmployee() -> show()
You could use controller routes.
Route::controller('emp', 'EmpController');
Now you just have to rename the functions within your controller to also represent the method used like this:
public function getAddEmloyee()
public function postAddEmloyee()
public function getEditEmployee($id)
etc.
See also the Laravel docs for controllers
Yes, use Route::match(). This will allow you to specify GET and POST in a single route call, like so:
Route::match(['GET', 'POST'], 'emp/edit-employee/{id}','EmpController#editEmployee');
You can also use Route::all() which will match any type request, which includes GET and POST and also any other HTTP verbs that may be specified, if that's what you want.
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 )
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.