Remove controller's name from url. Codeigniter - codeigniter

can someone help me with this. I want to remove controller name from the url like this
www.site-name.com/controller_name/controller_functions/controller_arguments
to this
www.site-name.com/controller_arguments
for instance:
www.site-name.com/blog/display/blog-title
to this:
www.site-name.com/blog-title
Below is my controller class
class Blog extends CI_Controller {
public function index() {
$data['blogs_data'] = $this->Blog_model->get_blogs();
$data['main_view'] = "blog/blog_layout";
$this->load->view('layouts/main', $data);
}
public function blog_display($page_url) {
$data['blog_data'] = $this->Blog_model->get_blog($page_url);
$data['main_view'] = "blog/blog_detail_layout";
$this->load->view('layouts/main', $data);
}
}
as your can see from my controller the URL of my site to display blog going to be like this www.website-name.com/blog/blog_display/($page_url) <== whatever the $page_url going to be. Now, what I want is to be like this www.website-name.com/($page_url) <= so straight to the $page_url.
Thanks

Go to application->config->routes.php
Then you can set a route to "blog/display/blog-title" as "blog-title"
Add the below line to your routes.php file
$route['blog-title'] = 'blog/display/blog-title';
You can replace "blog/display/blog-title" to "blog-title" then.

define route in routes.php file.
route['controller_arguments']='controller_name/controller_functions';
on which onclick you are showing this url.define there this.
<a href="<?php echo base_url(); ?>controller_arguments">
</a>

I imagine that your slugs are generated so you can't just write all your slugs into your routes.
In you specific case you need something like this in your routes:
$route['(:any)] = 'blog/display/$1';
Do bare in mind that your routes are used from top to bottom. So if you have this route as your first one the rest of your site might not work.
So in case of a entire blog stucture you might want something like:
$route[''] = 'blog/index'; // For first page without pagination
$route['(:num)] = 'blog/index/$1'; // Blog article pagination (for second page and all other pages)
$route['(:any)] = 'blog/display/$1'; // Blog article detail
To avoid some problems in the future with having a route that is just a (:any) param, you might want to add an extra segment in that blog detail article.
Like so:
$route['detail/(:any)] = 'blog/display/$1'; // Blog article detail
For more information about this subject take a quick look at the docs here: Codeigniter routing system

Related

how to hide id from URL laravel 7?

i have this url :
http://127.0.0.1:8000/deliverer/4
i want it like this
http://127.0.0.1:8000/deliverer/
this is my function :
public function show($id)
{
// $userhash->hashids->encode($id);
$user=User::find($id);
return view('deliverer.profile')->with('user',$user);
}
and this is my route
Route::get('deliverer/{id}', 'deliverer\DelivererController#show')->name('profile');
and this in view
<a href="http://127.0.0.1:8000/deliverer/{{ Auth::user()->id }}" >
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
Assuming, that what you're trying to accomplish is to view the current authenticated user's profile, then you should follow the steps below:
First you have to modify the route and remove the {id} from the URL, like this:
Route::get('deliverer', 'deliverer\DelivererController#show')->name('profile');
Then, inside the controller you have to remove the $id param from the show() method and change the method to get the id from the authenticated user.
public function show($id)
{
// $userhash->hashids->encode($id);
$user = \Auth::user();
return view('deliverer.profile')->with('user',$user);
}
And of course, you have to remove the Auth::user()->id() from the view route, and perhaps use the named route instead of hardcoding it, like so:
<a href="{{ route('profile') }}">
<i class="nc-icon nc-single-02"></i>
<p>Profile</p>
</a>
I'm assuming you're just trying to hide id's so users can guess the next number or try to pull up records they shouldn't.
Have you looked into UUID's? Example here: (https://dev.to/wilburpowery/easily-use-uuids-in-laravel-45be) That might be a solution.
Also, if you are worried that someone might tamper with URL to pull records, you should look into securing up your models. Do a check to see if the user should have access to that particular record. Many ways to accomplish that.
You can use token or configure a middleware, or as you did you can hash or crypt the id and make the verification after the call
The url will be like that :
http://127.0.0.1:8000/deliverer/?id=aze45a8sd54q

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

how construct route pattern for an unknown number of tags - Laravel & Conner/Taggable

I have a blog and a quotationfamous sayings repository on one site.
The quotations are tagged and the entries are tagged too.
I use this rtconner/laravel-tagging package.
Now, what I want to do is to display ALL Quotation models which share the same tags as article.
The Eloquent syntax is simple, as the original docs provide an example:
Article::withAnyTag(['Gardening','Cooking'])->get();
possible solution
Optional routing parameters. The asker-picked answer in this question gives a solution:
//in routes.php
Route::get('/{book?}/{chapter?}/{topic?}/{article?}', 'controller#func');
//in your controller
public function func($book = null, $chapter = null, $topic = null, $article = null) {
...
}
my problem
In my app the shared tags might count more than 3 or 5. I will soon get an example with even 10 tags. Possibly more
My question
Does it mean that I have to construct an URL with 10 optional routing parameters? Do I really need sth like this:
Route::get('quotations/tags/{tag1?}/{tag2?}/{tag3?}/{tag4?}/{tag5?}/{tag6?}/{tag7?}', 'controller#func');
my question rephrased
I could create a form with only a button visible, and in a hidden select field I could put all the tags. The route would be a POST type then and it would work. But this solution is not URL-based.
I think you could process the slashes, as data:
Route::get('quotations/tags/{tagsData?}', 'controller#func')
->where('tagsData', '(.*)');
Controller:
public function controller($tagsData = null)
{
if($tagsData)
{
//process
}
}
Ok, this is my solution. As I have a tagged model, I dont't need to iterate through tags in url to get the whole list of tags.
The enough is this:
// Routes file:
Route::get('quotations/all-tags-in/{itemtype}/{modelid}', 'QuotationsController#all_tagged_in_model');
Then in my controller:
public function all_tagged_in_topic($itemtype, $id) {
if($itemtype == 'topic') {
$tags = Topic::find($id)->tags->pluck('name')->all();
$topic = Topic::find($id);
}
if($itemtype == 'quotation') {
$tags = Quotation::find($id)->tags->pluck('name')->all();
$quotation = Quotation::find($id);
}
// dd($tags);
$object = Quotation::withAnyTag($tags)->paginate(100);;
And it is done.
Now, the last issue is to show tags in the URL.
TO do that, the URL should have an extra OPTIONAL parameter tags:
// Routes file:
Route::get('quotations/all-tags-in/{itemtype}/{modelid}/{tags?}', 'QuotationsController#all_tagged_in_model');
And in the {url?} part you can just write anything which won't break the pattern accepted by route definition.
In your view you might generate an URL like this:
// A button to show quotes with the same set of tags as the article
// generated by iteration through `$o->tags`
<?php
$manual_slug = 'tag1-tag2-tag3-tag4`;
?>
<a href="{{ URL::to('quotations/all-tags-in/article/'.$o->id.'/'.$manual_slug) }}" class="btn btn-danger btn-sm" target="_blank">
<i class="fa fa-tags icon"></i> Tagi:
</a>

Yield section if a certain controller is used

In my Laravel layout, I would like to display a menu bar if a certain controller is used.
How would I detect a controller inside blade?
For example:
// Layout main.blade.php
if(Controller == admin){
#yield('menu')
}
I know the syntax is wrong. Just to give you an idea what I'm trying to do.
There's a package for that: https://github.com/digithis/activehelper. It explains in detail how you can use it and it has helped me to do the same as what you're asking.
For a one-liner, you can use Route::currentRouteAction() assuming your routes are configured for example as:
Route::get('test', array('as'=>'test', 'uses'=>'TestsController#test'));
Very concrete for your question, you can use:
if (explode( '#' , Route::currentRouteAction())[0]) == 'controllerName')
{
// your code here
}

JToolbar::save() redirection

I'm going through the Joomla 2.5 tutorial to build a custom component. Now I'm facing an issue on the redirection after using JToolbar::save() or JToolBarHelper::cancel for that matter. By default Joomla wants to redirect to the default layout (from the edit layout). However I don't want it to do that. I want it to redirect back to another view. In Joomla 1.5 I would have done this through adding the function into the controller - something like
function cancel()
{
//redirects user back to blog homepage with Cancellation Message
$msg = JText::_( 'COM_BLOG_POST_CANCELLED' );
$this->setRedirect( 'index.php?option=com_jjblog&view=jjblog', $msg );
}
Now that works beautifully for the cancel function, however for save this is a much more complex thing. If I want to overwrite the url do I have to redirect the controller to the model and then write in all the code for the model interaction? Because that seems slightly excessive just for a url redirection like you would in Joomla 1.5?
Hope you have added the save toolbar code with the proper controller name like this
JToolBarHelper::save('controllerName.save');
Create a save function in appropriate controller.
Add the task in the form
Finnally make sure you have added form action withthe corresponding component name.
You can try this-
In the controller firstly you call the parent save function than redirect to url.
function save(){
parent::save();
$this->setredirect('index.php?option=com_mycomponent');
}
OK it didn't need to $this->setRedirect at all. Just needed me to change the value to
protected $view_list = 'jjBlog';
which then sets the redirects of everything back to that list view.
Source link for this is here.
Thanks for all the responses though!!
view.html.php
protected function addToolbar ()
{
JRequest::setVar ('hidemainmenu', false);
JToolBarHelper::title (JText::_ ('Configuration'), 'configuration.gif');
JToolBarHelper::save($task = 'save', $alt = 'JTOOLBAR_SAVE');
}
controller.php
public function save()
{
$mainframe = JFactory::getApplication();
$mainframe->enqueueMessage (JText::_ ('COM_SOCIALLOGIN_SETTING_SAVED'));
$this->setRedirect (JRoute::_ ('index.php', false));
}
I think you can use
global $mainframe;
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
If you are overriding joomla's default save function in your custom component like
function save( $task = 'CustomSave', $alt = 'Save' ) // or even same name Save
Inside your controller you can use the CustomSave as the task and use $mainframe for redirect.
or
$mainframe = &JFactory::getApplication();
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
Hope this may help you..

Resources