How to fix CodeIgniter routes - php

I am new in framework please help to solve this. I am working in CodeIgniter. I want to have clean URLs like this: example.com/index.php/6/title
index.php/6 is a post ID and title is a Page title.
I want to get data by post ID.
Controller function:
public function watch(){
echo id;
}
How to get this clean URL using routes. I am trying this route but it's not working:
$route['(:num)/(:any)'] = 'front_controller/watch/';

Try this for routing to watch() function in Front_controller with 2 parameters id and title
$route['(:num)/(:any)'] = 'front_controller/watch/$1/$2';
Also you should accept 2 paramets in your controllers function too as :
function watch($id,$title)
{
echo $id;
echo $title;
}
Let me know in case of any queries.

Related

Laravel - Passing parameter for patch request

Hi i am working on a update method for updating a profile, right now i am updating the profile by passing the model as a parameter but i am wanting to pass in the id of a profile so the route is patch('/profiles/podcast/{id}'' i am quite new to laravel so im wondering how do i modify the controller and phpunit test to update this way when grabbing objects in laravel?
Update function in the controller:
public function update(PodcastProfile $podcastProfile)
{
$this->user = Auth::user();
$this->podcastProfile = $podcastProfile;
if (!$this->hasPodcastProfile()) {
abort(400, "You don't have a podcast profile configured");
}
$this->validation();
$this->transaction();
return $this->podcastProfile->toJson();
}
This is the current route for the update method
Route::patch('/profiles/podcast/{podcastProfile}', 'PodcastProfileController#update');
This is the phpunit test case for the function
/**
* #test
*/
public function it_should_update_podcast_profile()
{
$podcastDetails = $this->handlePostRequestToController();
$this->json('patch', '/profiles/podcast/' . $podcastDetails['id'], $this->updateData)
->assertSuccessful();
$this->checkPodcastProfile($this->updateData, $podcastDetails['id']);
$this->checkGuestFormats($this->updateData['guest_format']);
$this->checkAvailability($this->updateData['availability']);
$this->checkEquipment($this->updateData['equipment']);
$this->checkCategories($this->updateData['categories']);
$this->checkLocation($this->updateData['country'], $this->updateData['city']);
}
Hej,
if I understand your question correctly you would just pass the id of that profile instead, just like you said:
Route::patch('/profiles/podcast/{podcastProfileId}','PodcastProfileController#update');
and then fetch the profile by that given id.
so something like:
$this->podcastProfile = App\PodcastProfile::find($podcastProfileId)
Also i feel like choosing the route-model-binding approach like #Remul described would be the better approach.

How to send data/variable from controller to other controller in codeigniter without using session?

How could I send data/variable from controller to other controller in codeigniter without using session?
I know how to send data from controller to other controller using session but I don't want to use it as it's giving me problem. If I use session, data I need to send will be used in many pages.
Example of the Data from controller 1 is $id (which I want to use on the other controller), but in the controller 2. If I open many pages, I only get the same data which is not what I expected. In controller 2, if I'm in page 1 I need $id = 1 while if I'm in page 2 I need the $id = 2. Any help with me appreciated. Thanks in advance.
GET -- It's your choice ?
if yes then.. use URI Class
http://example.com/index.php/news/local/1
segment(1) = "news"
segment(2) = "local"
segment(3) = "1" <-- your $id
Controller
$id = $this->uri->segment(3);
test.php Controller File :
Class Test {
function demo() {
echo "Hello";
}
}
test1.php Controller File :
Class Test1 {
function demo2() {
require('test.php');
$test = new Test();
$test->demo();
}
}
But using require like this in codeigniter is not a good idea...

Passing parameters in CodeIgniter

I have been banging my head against this problem for about an hour now. I have looked high and low but nothing is working for me. This should be simple and I am sure it is.
I am trying to pass some parameters in CodeIgniter to a URL and nothing seems to be working. Here is my controller:
class Form_controller extends CI_Controller {
public function change($key = NULL) {
if (is_null($key)) {
redirect("reset");
} else {
echo "Hello world!";
}
}
}
Here is my route:
$route['change/(:any)'] = "form_controller/change/$1";
Every time I visit /index.php/change/hello I get the string "Hello world!" but when I visit /index.php/change I got a 404 not found.
What I am trying to do is pass a parameter to my controller for the purposes of checking the DB for a specific key and then acting upon it. If the key does not exist in the DB then I need to redirect them somewhere else.
Any thoughts on this?
Never mind, I figured it out. I ended up making two different routes to handle them, like so:
$route['change'] = "form_controller/change";
$route['change/(:any)'] = "form_controller/change/$1";
And the function in the controller looks like this now:
public function change($key = NULL) {
if (is_null($key)) {
redirect("reset");
} else if ($this->form_model->checkKey($key)) {
$this->load->view("templates/gateway_header");
$this->load->view("forms/change");
$this->load->view("templates/gateway_footer");
} else {
redirect("reset");
}
}
If anyone has a better solution, I am all ears. This worked for me though.
This might help you out
public function change() {
$key = $this->uri->segment(3);
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
This allows you to grab the segment easily using the CI url helper
index.php/change/(3RD Segment) this part will go into that variable $key.
This may not be what you are looking for but it is very useful if you are trying to pass two variables or more because you can just grab the segment from the url and store it into the variable

Codeigniter URI: Creating Default Controller function

I have the following class in my controller (codeigniter)
class Books extends CI_Controller {
function index($id = NULL){
//model
//view
}
}
I have this link in my view file
/books/index/<? echo $id ;?> > Book1
when I click on the above link the URL in the address bar looks like >
http://localhost/my_web/books/index/1
But I am trying to make the URL look like-
http://localhost/my_web/books/1
So, after studying this tutorial, in my application/config/routes.php I used the following code.
$route['books/:num'] = "books/index";
And then I changed my link to following code, but when I click on it, the page says 404 Page not found
/books/<? echo $id ;?> > Book1
Could you please tell how to achieve this?
Thanks in Advance :)
you are missing parameter in routes, Try:
$route['books/(:num)'] = "books/index/$1";
Set in the Route which is application/config/route.php
$route['books/(:any)'] = "books/index/$1";
it definitely work for you route.

How does posting query parameters in CodeIgniter work?

I have this format on links:
blah/link/11
where blah is the controller and link is a function inside it. But now I want to send a number in the querystring. In normal non-MVC way I would have done like this:
page.php?id=11
So what should I do for getting the eleven in my link function?
class Blah extends Controller {
function link( $id ) {
// $id == 11
}
}
reachable via URL blah/link/11
There may be other ways to go about this, but it looks like CodeIgniter has a URI Class that will allow you to retrieve specific segments of your URI. So something like
$id = $this->uri->segment(3); //from a controller, I assume
should get you what you want.
It also looks like CodeIgniter will take additional URI parameters and pass them through as parameters to your action function.
#http://example.com/index.php/products/shoes/sandals/123
class Products extends Controller {
function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}

Categories