Create "Permalink" functionality in codeigniter - codeigniter

Recently i am working with codeigniter. I need to create a permalink functionality just like as wordpress. Can anyone suggest me a way that, how i can implement this in codeigniter.

Can you give more details about what you want to do.
In my understanding you want to create something like this?
http://example.com/2012/post-name/
1) You must have a mod_rewrite.
2) Use the controller or model but will advice to use Controller
Controller:
public function something(string name, int year){
//some code
}
This will be translated to
http://www.example.com/something/name/year
Hope this helps or you can paste your code so I can explain further.

So create the page: admin.php
Then in the controller:
public function about-us(){
$this->view->load('about-us');
}
When you run the page is will be: http://domain.com/about-us. about-us is the function in the controller.

Are you asking how to remove the spaces and replace with a hyphen?
$slug = 'this is a bad slug';
$fixed_slug = str_replace(' ', '-', $slug);
# uncomment next line to see result
# echo $fixed_slug;
If you want to make that a method, no worries:
public function fix_slug($slug){
$fixed_slug = str_replace(' ', '-', $slug);
return $fixed_slug;
}
You can obviously do more in that method, or abstract it to use with other text you want altered.

Related

CodeIgniter URI Routing: remove method name index from URL

I am working with CodeIgniter (V:2.2.6) and I have a simple class User with some basic methods like create, update, edit, delete and index. For the index function, I am using an argument $user (which is the second URI segment) in order to display some information regarding that user. So the default URL looks like:
/user/index/john
to display some information about the user 'john'.
Now, I want to remove the term 'index' from the URL, so that it looks like:
/user/john
For that purpose I have added the following rule in routes.php.
$route['user/(:any)'] = "user/index/$1";
It serves the purpose, but it prevents accessing other functions like /user/create and goes inside /user/index automatically. To solve this problem, I can not see any other way except manually adding routing rules like
$route['user/create'] = "user/create";
for each method of the User class, which is not cool at all. So, please can anyone suggest me a better way of routing under the current circumstances?
Here are my codes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index($user = '') {
echo "index!";
}
public function create() {
echo 'create!';
}
}
Note: I have gone through the CodeIgniter documentation for URI Routing and another similar question here, but could not figure out a promising solution. And please don't suggest for CodeIgniter version update.
I'm not sure, if this is the answer you like, but i think it should work.
Just put this function in your user controller.
public function _remap($method)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index($method);
}
}

Laravel search by dates not working

I need to search by date. My route definition:
Route::get('/calendar/days?day={date}', 'HomeController#getDays');
In the controller method I have this:
public function getDays($date)
{
$articles = Article::where('published_at','=',$date)->get();
dd($articles);
}
When you click on the link on the image below I get a NotFoundHttpException.
The link is generated using JS. Could this be the problem?
You should not be defining query string parameters in the route definition. For a detailed explanation on why that is see this answer. So in your case you have two options:
1. Remove the ?day={date} from the definition:
Route::get('/calendar/days', 'HomeController#getDays');
And in your controller access the request input parameter like so:
use Illuminate\Http\Request;
...
public function getDays(Request $request)
{
$date = $request->input('date');
$articles = Article::where('published_at', '=', $date)->get();
}
2. Modify your route definition to something like this:
Route::get('/calendar/days/{date}', 'HomeController#getDays');
The controller code you have now needs no change in this case, however the link you generate via JavaScript would need to look like this:
9

changing the url into dash instead of underscore using Codeigniter

Hi im using codeigniter framework because its really cool when it comes to MVC thing. I have a question regarding in the url thing. Usually when saving a controller, lets say in the about us page it has to do something like this About_us extends CI_Controller. When that comes to the url thing it goes like this test.com/about_us. I want that my url is not underscore. I want to be dashed something like this test.com/about-us. How will i able to use the dash instead of using underscore???
any help is greatly appreciated
thanks!
Code Ignitor 3 has this built-in option. In your routes.php:
$route['translate_uri_dashes'] = FALSE;
Just change this to "TRUE" and you can use either _ or - in URL
Here is a detailed article how to fix it.
You will just have to create routes for your pages, as far as I am aware, there is no config directive to change the separating character, or replace '-' with '_' in CI_Router, (probably wouldn't be too hard to add this).
To allow for '-' instead of '_':
Create a file in application/core/MY_Router.php
Change 'MY_' to whatever value is specified in your config directive: $config['subclass_prefix']
insert code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function _set_request($segments = array()) {
if (isset($segments[0]))
$segments[0] = str_replace('-','_',$segments[0]);
if (isset($segments[1])) {
$segments[1] = str_replace('-','_',$segments[1]);
}
return parent::_set_request($segments);
}
}

Laravel 4 parameter as controller action

I would like to know if there is a possibility in Laravel 4 to actually call function of controller based on parameter given. For example if I have route like :
'auth/{action}
then is there a way to call controller action based on 'action' param ? In Kohana I could write something like:
'auth/<action>' -> defaults (controller=>'UserController',action=>'<action>'
Well not exacly like that but you know what I mean :) Anyway if there is no chance to do that then do I have to split my route to single routes ?
Sounds like you just need to route to the controller with auth being the base URI.
Route::controller('auth', 'AuthController');
This controller (AuthController) now expects your methods to be prefixed with the HTTP verb they should respond to. You can also use the getIndex method to respond to the base URI, which in this case is auth.
An example controller might look something like this:
class AuthController extends Controller {
public function getIndex()
{
return 'Index page'; // Responds to localhost/auth
}
public function getLogin()
{
return 'Login page'; // Responds to localhost/auth/login
}
}
There is one thing you should be aware of. If you do Route::controller('/', 'HomeController'); then it should be LAST. Any routes after it will not get called because of Laravel automatically adding a "missing method" route that will catch anything that isn't matched by a routable method on the controller.
More on RESTful controllers at the official documentation.
I haven't tested this exact code, but I used something similar and it worked:
Route::any('auth/{action}', function($action){
$controller = new UserController();
$controller->$action();
});
You might find you also need to handle parameters, like this:
Route::any('auth/{action}/{param}', function($action, $param){
$controller = new UserController();
$controller->$action($param);
});
You could even tweak it to cover all your controllers:
Route::any('{controller}/{action?}/{param?}', function($controller,$action='index',$param=null)
{
$controller = str_replace(' ', '', ucwords(str_replace('-', ' ', $controller))).'Controller';
$controller = new $controller;
$action = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $action))));
return $controller->$action($param);
});
If you wanted to have an index action with a parameter, that wouldn't work, but otherwise, it seems to work well enough. It's also not going to handle a second parameter, if you were wanting to do that.
Lots of ways you could extend this idea.
Jason's answer is more correct, (matches the docs, cleaner code, etc.) but if you didn't want to think about HTTP methods, or you wanted a sort of master route to handle nearly every request, this is an option.

accessing lang variables from controller + codeigniter

How can I access my lang variables in controller?
function index()
{
$seo_title = $this->lang->line('blablabla');
$data['page_title'] = $seo_title;
$this->load->view('contact/index.php',$data);
}
in my lang file blablabla has string in, and it works well when I call from view file. but when I want to call from controller, it doesnt return anything :(
any idea about problem? appreciate!!
You need to load the language file you want to use before you grab lines from it:
$this->lang->load('filename', 'language');

Resources