Codeigniter URI: Creating Default Controller function - php

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.

Related

Laravel | Call variable in view

I need help figuring out what I'm doing wrong. I'm building a chat where on the right side I have the users and when clicking on the user opens the respective chat.
In the users on the right I put the following HREF:
<a href="{{route('chat.index', ['id'=>$item->id])}}">
Which then shows the following link on the page:
/Chat?id=10
I put in the view if there is id shows the chat.
#if ($id)
But don't come here. I can't get the IF to be true.
In the controller I have the following code, but I don't think it's wrong.
$outroUser = null;
if($id){
$outroUser = utilizador::findOrFail($id);
}
$tabela = utilizador::orderBy('id', 'desc')->get();
return view('painel-admin.chat.index', ['itens' => $tabela, $outroUser]);
To get id param you can try following in your controller
if ($request->has('id')) {
$outroUser = utilizador::findOrFail($request->query('id'));
}
Do not forget to add Request object into your controller function params
use Illuminate\Http\Request;
public function yourController(Request $request) {
//Code
}

"Type error: Too few arguments to function App\Http\Controllers\FrontController::detail(), 0 passed and exactly 1 expected"

I want to passing a data from Shirts to details, to know a detail of product from Shirts. How to do that? I have some error here
Error's Message
This my web view
web View
FrontController.php
class FrontController extends Controller
{
public function index()
{
$shirts=Product::all();
return view('front.home', compact('shirts'));
}
public function shirts()
{
$shirts=Product::all();
return view('front.shirts', compact('shirts'));
}
public function detail($id)
{
return view('front.shirt', ['detail' => Product::findOrFail($id)]);
}
Route
Web.php
Route::get('/', 'FrontController#index')->name('home');
Route::get('/shirts', 'FrontController#shirts')->name('shirts');
Route::get('/detail', 'FrontController#detail')->name('detail');
Shirts.blade.php
<a href="{{route('detail', $shirt->id)}}">
you missing argument in route('detail')
Edit to:
Route::get('/detail/{id}', 'FrontController#detail')->name('detail');
And Try again.Hope this help :D
there is two three thing that need to change first is route file
it should be like this
Route::get('/detail/{$id}', 'FrontController#detail')->name('detail');
from this you get that parameter $id that you passes in controller .
and for view as per i see in your code there your route is
<a href="{{route('detail', $detail->id)}}">
which has to be like this
<a href="{{route('detail', $shirt->id)}}">
i think it might solve your problem
hope it will help you
I think in your Route you need to give Id parameter also,
Route::get('/detail/{id}', 'FrontController#detail')->name('detail');

How to fix CodeIgniter routes

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.

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...

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