Codeigniter route : how to show name in url instead of id number - codeigniter

I need some help with url in codeigniter..
when I click on Details for a particular item the url is like this...
http://localhost/testproject/index.php/Item/details/2
is it possible to display like this?
http://localhost/testproject/index.php/Item/samsung-galaxy-s7
results are coming from the database. say my database look like this...
item_id Item_name
------------------------
1 Nokia Lumia 5
2 samsung galaxy s7
My controller "Item", and function "details"
public function details($item_id=0)
$data['itm_details']=$this->Item_model->getDetails($item_id);
$data['view_page']='item/details_page';
$this->load->view('homepage', $data);

I think its not possible to click a url then change it after but One thing you can do is to pass the item name at the url then urldecode the given string.
CONTROLLER
public function details($item_name=NULL){
$item_name = urldecode($item_name);
$data['itm_details']=$this->Item_model->getDetails($item_name);
$data['view_page']='item/details_page';
$this->load->view('homepage', $data);
}
Just an example
function temp(){
echo "<a href='".site_url('controller/temp_redirect/this sample_-url')."'>Click Me</a>";
}
function temp_redirect($url){
echo urldecode($url);
}

Related

how can i make 4 links to a controller?

i'm setting up a new project in my laravel
The stuffs i have :
1- PostsController
2- Post model
3- Routes:resource('posts','PostsController')
4- after i login i have a create button for post create
after click on it i have below image :
My desire:
i want without changing whole project or whole routes resources have these :
after i login i want to have 4 boxes like this( i made it )
with this property :
after i click any of them i can create post according to their type
for example if i click on video content i enter to a page with 2 form:
like title video and upload video
after i click on text content i enter to page like the image i showed
you at the first
it means if i click on video content, resource route take me to create method in postscontroller and create method check if i came from content video link
return view (posts.create_video) or if i came from sound content link box return view(posts.content_sound) and etc boxes
how can i do all of that please help me thanks
You could structure it as following:
[routes/web.php]
Route::get('posts/create/{type}', 'PostController#create')->name('posts.create');
Route::resource('posts', 'PostController')->except(['create']);
[PostController]
class PostController extends Controller
{
public function create($type)
{
if (in_array($type, ['sound', 'video', 'image', 'text'])) {
return view("posts.content_{$type}");
}
abort(404);
}
}

Laravel 5.5 How to read flash data in a view page

My Controller is redirecting to this View with flash data(data from a table row). How do I print for example, just the "titulo" or "resumo"?
Or, instead of sending all the information from a table row to a single variable, do I have to send separately into different variables?
Thanks in advance.
This is the way you can send flash data from the backend:
public function show (){
//some code here
return redirect('/')->with('flash', 'message here');
}
and in the view you can display it like this:
{{session('flash')}}
The above will display "message here".
If you have return redirect('/')->with('alert', 'something happened');
{{session('alert')}}
The above will display "something happened".

Laravel view product page not found

I have a problem, doing some product page, already got listen all products from database. And now doing view object page, to check currently product details. Doing everything by this video:
'https://www.youtube.com/watch?v=SpOaqDJ2D3A'
But it's not working for me. Writing page not found when I press to a link View product.
1. I have created: view_object.blade.php
2. Have a controller:
'public function view_brac( $BrackID )
{
return view('view_object');
}'
3. Have a route:
'Route::get('view_object&(BrackID?)','BracController#view_brac');'
4. And this is a link to View full object:
'< a href="view_object& < ?php echo $ users->BrackID? >">Plačiau< /a >'
Can some you help, why this not working?
If you want to use URI with parameter like view_object?brackId=5, change the route to:
Route::get('view_object', 'BracController#view_brac');
The controller method to:
public function view_brac()
{
return view('view_object');
}
And the link to the view:
Plačiau
Then you'll be able to get the brackId in a view or a controller method with:
{{ request('brackId') }}
First, you aren't returning anything in your controller method along with the view.
public function view_brack( $BrackID )
{
$brack = Brack::find($BrackID);
return view('view_object', compact('brack'));
}
Second, the route definition is incorrect.
Route::get('brack/{brack}','BracController#view_brack')->name('brack.show');
Last, create your anchor tag using Laravel's helper functions.
Plačiau

Passing Id from one view to another when Button is clicked in Laravel 5.4

I am using response from xml server. I am trying to display the different detail view when each BUtton is pressed respectively.When the Button is pressed, I want to send the Id of each button to the detailed page and I want to display that Id on the detailed page too.
My Button for clicking and passing ID to next view respective to each button clicked is:
<a href='{{route('Detailed',["Id" => $flight["Id"]])}}'>SELECT NOW</a>
$flight["Id"] is the response value that I get from the xml server
My route is for passing Id is: Id is what I want to pass from one view to another and display in second view
Route::get('/Detailed/{Id}',[
'as' => 'Detailed', 'uses'=>'FlightController#show'
]);
My controller function to display is:
public function show($Id)
{}
It gives me error saying that:
Missing required parameters for [Route: Detailed] [URI: Detailed/{Id}] in view.blade.php.
Please anyone help me
But if give direct value in button route
such as ID value then,
<a href='{{route('Detailed',["Id" => "1234"])}}'>SELECT NOW</a>
It works exactly what I expect.
Notes the Id value that I get is from simple_xml_response from server.
Try this:
Route
Route::get('detailed/{id}', 'FlightController#show')->name('detailed');
Controller
public function show($id) {}
Blade
<a href='{{route('detailed', '1234')}}'>SELECT NOW</a>

How do you use JRoute in Joomla to route to a Search menu item?

I am trying to create a a box in a template in Joomla! that will display all of the keywords and link them to their appropriate search page. I have a menu item set, however, I don't want to hard-code the menu item into the template, so I want to use the JRoute object to generate the SEF url.
I am using this function:
JRoute::_('index.php?option=com_search&searchword='.$keyword);
or this:
JRoute::_('index.php?option=com_search&view=search&searchword='.$keyword);
however, this generates a url like this:
/component/search/?searchword=africa
when it ought to create a search url like this:
/searchmenuitem?searchword=africa
I have searched extensivly online and havn't found a solution to this problem. Any ideas would be greatly appreciated.
Ok, so some additional information for you.. I am only experiencing the problem when I try and route the URL from a template in com_content. If I try and route the url from a template in com_search everything works perfectly. So, what is it about com_content that is causing this to not work properly?
thanks!
david
In joomla administration page go to the menu item you've chosen for the search results page and get the id of that menu item (itemId).
Than you can try using:
JRoute::_('index.php?option=com_search&view=search&Itemid=256&searchword=asdsadasdsa');
or even
JRoute::_('index.php?Itemid=256&searchword=asdsadasdsa');
both should result in: /searchmenuitem.html?searchword=asdsadasdsa
EDIT:
To make it more comforable you could add itemId as a param to your template.
There is another way, where u can get the itemId from the database (this method is required on multilingual websites). Let me know if you want it.
EDIT2:
Here it is:
$db =& JFactory::getDBO();
$lang =& JFactory::getLanguage()->getTag();
$uri = 'index.php?option=com_search&view=search';
$db->setQuery('SELECT id FROM #__menu WHERE link LIKE '. $db->Quote( $uri .'%' ) .' AND language='. $db->Quote($lang) .' LIMIT 1' );
$itemId = ($db->getErrorNum())? 0 : intval($db->loadResult());
I use this kind of method to get a menu item id of specific component and view
function getSearchItemId() {
$menu = &JSite::getMenu();
$component = &JComponentHelper::getComponent('com_search');
//get only com_search menu items
$items = $menu->getItems('componentid', $component->id);
foreach ($items as $item) {
if (isset($item->query['view']) && $item->query['view'] === 'search') {
return $item->id;
}
}
return false;
}
Then I use this method to get the sef url
function getRouteUrl($route)
{
jimport('joomla.application.router');
// Get the global site router.
$config = &JFactory::getConfig();
$router = JRouter::getInstance('site');
$router->setMode($config->getValue('sef', 1));
$uri = &$router->build($url);
$path = $uri->toString(array('path', 'query', 'fragment'));
return $path;
}
This just works in any template.
use like this
$itemid = getSearchItemId();
//returns valid sef url
$url = getRouteUrl('index.php?Itemid='.$itemid);
You really do not need to do sql on the menu table to get ids. Just search the menu object.
Try to create new menu in the joomla backend called for instance 'hidden-menu'. It will never be shown in the front. But it will be used by JRoute Then add to this menu new menuitem called 'searchmenuitem' with link to com_search. That is all. Now you can call
JRoute::_('index.php?option=com_search&view=search&searchword=asdsadasdsa');
and it will be ceonverted into this
/searchmenuitem.html?searchword=asdsadasdsa

Resources